关于如何使自定义的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的自定义设置。

posted @ 2014-09-05 11:54  醉云天  阅读(1246)  评论(0编辑  收藏  举报