UIButton那些应用细节

1、消除UIButton的高亮状态:
(1)自定义button
(2)重写Highlighted的set方法

1 - (void)setHighlighted:(BOOL)highlighted{};

2、修改button内部控件:UIImageView和UILabel的位置
(1)方法一:
   1】自定义button;
   2】重写以下方法:

1 // 重新设置ImageView的frame
2 - (CGRect)imageRectForContentRect:(CGRect)contentRect
3 {
4 return imageRect;
5 }
6 // 重新设置Label的frame
7 - (CGRect)titleRectForContentRect:(CGRect)contentRect
8 {
9

(2)方法二:
   1】自定义button;
   2】重写LayoutSubviews方法:

 1 // 此举例实现的功能是让button内部的imageView和label上下排布
 2 - (void)layoutSubviews
 3 {
 4  [super layoutSubviews];
 5  CGFloat titleH =21; CGFloat imageX =0;
 6  CGFloat imageY =0;
 7  CGFloatimageW = self.bounds.size.width;
 8  CGFloat imageH =self.bounds.size.height-titleH;
 9  self.imageView.frame = CGRectMake(imageX,imageY, imageW, imageH);
10  self.titleLabel.frame = CGRectMake(imageX,imageH, imageW, titleH);
11 }

3、设置文字图片居中

1 self.titleLabel.textAlignment = NSTextAlignmentCenter;
2 self.imageView.contentMode= UIViewContentModeCenter;

4、设置label换行显示

button.titleLabel.numberOfLines = 0;

5、设置尺寸随内容尺寸而定:

1 [btn sizeToFit];


6、让navigationbar的leftItem或rightItem尽量靠近屏幕边缘:

1 // 自定义button---MainTagSubIcon(测试图片名称)
2 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
3 [button setImage:[UIImage imageNamed:@"MainTagSubIcon"] forState:UIControlStateNormal];
4 // 设置button的尺寸
5 [button sizeToFit];
6 // 设置内边距对应的位置为负数
7 button.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
8 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
原文地址:https://www.cnblogs.com/jfckliving/p/4768889.html