关于如何使自定义的Button和系统的UIBarButtonItem保持一致的两种方法

  在使用过程中,总是会遇到需要自定义UINavgationBar或者是UIToolBar的UIBarButtonItem,但通常我们所得到的结果是自定义之后的button的title和系统的有所区别;下面是两种降低这种区别的方法:

<一> 使用自定义Button,将系统的字体颜色抽取出来,使用Button进行设置,下面所抽取出来的颜色比例与系统的基本一样;

UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem *buttonItem1 = [[UIBarButtonItem alloc]initWithTitle:@"one" style:UIBarButtonItemStyleDone target:self action:nil];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, 60, 60);
    [button setTitle:@"上传" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:22];
    [button setTitleColor:[UIColor colorWithRed:4/255.0 green:112/255.0 blue:253/255.0 alpha:1] forState:UIControlStateNormal];
    UIBarButtonItem *buttonItem2 = [[UIBarButtonItem alloc]initWithCustomView:button];
    NSArray *array = [[NSArray alloc]initWithObjects:buttonItem1,spaceButton,buttonItem2, nil];
    [self setToolbarItems:array];

 

<二> 将系统的UIBarButtonItem的字体样式向自定义Button的字体样式靠近,设置UIBarButtonitem的appearance属性;

[[UIBarButtonItem appearance]setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:22], NSFontAttributeName,nil]  forState:UIControlStateNormal];

 或者是

UIBarButtonItem *appearance = [UIBarButtonItem appearanceWhenContainedIn:[UIToolbar class], nil];
    [appearance setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:25], NSFontAttributeName,nil] forState:UIControlStateNormal];

 

基于以上方法,基本能够实现你对界面UIBarButtonItem的自定义设置。

转载于:https://www.cnblogs.com/yuanjianguo2012/p/3957765.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值