- 1序苏、消除UIButton的高亮狀態(tài):
- 1)自定義button
- 2)重寫Highlighted的set方法
- (void)setHighlighted:(BOOL)highlighted{};
2、修改button內(nèi)部控件:UIImageView和UILabel的位置
-
1)方法一:
- 1】自定義button捷凄;
- 2】重寫以下方法:
// 重新設(shè)置ImageView的frame - (CGRect)imageRectForContentRect:(CGRect)contentRect { return imageRect; } // 重新設(shè)置Label的frame - (CGRect)titleRectForContentRect:(CGRect)contentRect { }
2)方法二:
1】自定義button忱详;
2】重寫LayoutSubviews方法:
// 此舉例實(shí)現(xiàn)的功能是讓button內(nèi)部的imageView和label上下排布
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat titleH =21; CGFloat imageX =0;
CGFloat imageY =0;
CGFloatimageW = self.bounds.size.width;
CGFloat imageH =self.bounds.size.height-titleH;
self.imageView.frame = CGRectMake(imageX,imageY, imageW, imageH);
self.titleLabel.frame = CGRectMake(imageX,imageH, imageW, titleH);
}
- 3、設(shè)置文字圖片居中
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.imageView.contentMode= UIViewContentModeCenter;
- 4跺涤、設(shè)置label換行顯示
button.titleLabel.numberOfLines = 0;
- 5匈睁、設(shè)置尺寸隨內(nèi)容尺寸而定:
[btn sizeToFit];
- 6监透、讓navigationbar的leftItem或rightItem盡量靠近屏幕邊緣:
// 自定義button---MainTagSubIcon(測(cè)試圖片名稱)
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"MainTagSubIcon"] forState:UIControlStateNormal];
// 設(shè)置button的尺寸
[button sizeToFit];
// 設(shè)置內(nèi)邊距對(duì)應(yīng)的位置為負(fù)數(shù)
button.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];