自定義UIBarButtonItem

lf644206005發表於2015-07-27

自定義UIBarButtonItem

我們經常使用UIBarButtonItem的時候,都會發現本機的不能滿足我們的要求。
直接上程式碼:

UIBarButtonItem *cancleBT = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [self.navigationItem setRightBarButtonItems:@[cancleBT, [[UIBarButtonItem alloc] initWithCustomView:self.cancelBtn]]];
#pragma mark ---cancleBT
-(UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        [_cancelBtn setTitleColor:UIColorFromHex(0x2298f3) forState:UIControlStateNormal];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(cancleSearch) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

相關文章