内容简介:作者:雪山飞狐_91ae链接:https://www.jianshu.com/p/c4979c205024
我们在写代码自定义UIBarButtonItem的时候,一般是像下面这样来定义的:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"qr-code"] style:UIBarButtonItemStylePlain target:self action:nil];
这样创建就产生了一个问题,就是创建的这个UIBarButtonItem它的高度是充满UINavigationBar的,有一次PM就觉得这样上下充满UINavigationBar不是很好看,要我把这个UIBarButtonItem的尺寸调小一点(当然PM不知道这是UIBarButtonItem,它只知道这是个控件哈哈)。
下面讲的就是如何调整UIBarButtonItem的size。
步骤:
-
1.创建一个自定义的控件,比如UIButton,后面我们需要用这个Button来创建UIBarButtonItem。
-
2.约束这个Button的size。
-
3.利用这个button来创建UIBarButtonItem。
创建一个自定义的控件,这里以UIButon为例
这一步和创建普通的控件没有区别:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];UIImage *image = [UIImage imageNamed:@"小qr-code"]; [button setImage:image forState:UIControlStateNormal]; [button addTarget:self action:@selector(weChatScanlogin) forControlEvents:UIControlEventTouchUpInside];
约束这个控件的size
测试的时候发现给这个Button设置frame是没有用的,仍然会充满UINavigationBar,我们需要通过下列方式来约束控件的width和height:
[button.widthAnchor constraintEqualToConstant:35].active = YES; [button.heightAnchor constraintEqualToConstant:35].active = YES;
利用这个控件来创建UIBarButtonItem
UIBarButtonItem有一个实例方法:
- (instancetype)initWithCustomView:(UIView *)customView
这个方法可以用来通过自定义的控件创建UIBarButtonItem。
IBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
作者:雪山飞狐_91ae
链接:https://www.jianshu.com/p/c4979c205024
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Discrete Mathematics and Its Applications
Kenneth H Rosen / McGraw-Hill Science/Engineering/Math / 2003-04-22 / USD 132.81
Discrete Mathematics and its Applications is a focused introduction to the primary themes in a discrete mathematics course, as introduced through extensive applications, expansive discussion, and deta......一起来看看 《Discrete Mathematics and Its Applications》 这本书的介绍吧!