UINavigationcontrollerBar,UIbarButtonItem按钮以及backButton背景及背景颜色改变

来源:互联网 发布:mac os app store 编辑:程序博客网 时间:2024/04/29 20:58


(作者比较懒,喜欢用iOS自带属性修饰控件,喜欢自定义或重写的朋友请绕行)
一.自定义navigation外观
先准备背景图片navigation
 UIImage *image = [UIImageimageNamed:@"navigation"];

    //改变navigationcontroller背景(青色)

    [self.navigationController.navigationBar  setBackgroundImage:imageforBarMetrics:UIBarMetricsDefault];

    UIBarButtonItem *back = [[UIBarButtonItem  alloc]initWithTitle:@"back"style:UIBarButtonItemStyleBorderedtarget:self  action:@selector(back:)];

    //backButton背景颜色(back按钮背景黄色)

    back.tintColor = [UIColor  yellowColor];

    self.navigationItem.backBarButtonItem = back;


    UIBarButtonItem *right = [[UIBarButtonItem  alloc]initWithTitle:@"next"  style:UIBarButtonItemStyleBordered  target: self  action:@selector(next:)];

    //改变right背景颜色(next按钮背景绿色)

    right.tintColor = [UIColor greenColor];

  self.navigationItem.rightBarButtonItem = right;


  //    NSArray *fontsArr = [UIFont familyNames];//打印所有字体

    //navigation标题(棕色brownColor)(字典依次为标题(餐饮,种类)字体颜色,阴影颜色,阴影范围,字体,字体大小)

    [self.navigationController.navigationBarsetTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:[UIColorbrownColor],UITextAttributeTextColor, [UIColorcolorWithRed:0 green:0blue:0alpha:0.8],UITextAttributeTextShadowColor,[NSValuevalueWithUIOffset:UIOffsetMake(0.5, 0.5)],UITextAttributeTextShadowOffset,[UIFontfontWithName:@"Times New Roman"size:24.0f],UITextAttributeFont,nil]];

      //定制应用中所有导航栏有barButttonItem按钮  appearance(字典依次为标题(next,back)字体颜色,阴影颜色,阴影范围,字体,字体大小)

    [[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:

        [UIColor redColor ],UITextAttributeTextColor,[UIColorcolorWithRed:0green:0 blue:0alpha:0.8],UITextAttributeTextShadowColor,[NSValuevalueWithUIOffset:UIOffsetMake(1.5, 1.5)],UITextAttributeTextShadowOffset,[UIFontfontWithName:@"Times New Roman"size:20],UITextAttributeFont,nil]forState:UIControlStateNormal ];

有图有真相:(为了显示阴影效果偏移量UIOffsetMake(1.5, 1.5)比较,自己调小即可,红色为按钮标题颜色,黑色(alpha:0.8)为阴影)



二.图片+手势也可完成navigationController同样功能
0 0
原创粉丝点击