zoukankan      html  css  js  c++  java
  • UIBarButtonItem关于全局修改,局部修改

    全局修改:把所有UIBarButtonItem(或者一个控件)设为同一风格。

    局部修改:根据一定条件把一部分UIBarButtonItem(或者一个控件)设为同一风格

    有时侯你想把导航条左侧的所有按钮的外观,字体设置为同一风格,但你并不想把导航条左侧按钮外观字体或背景全部用以下代码来更改,如果这样改,有两个UIBarButtonItem,你就要写两次,这样写代码过于赘余,苹果提供了更好的方法统一设置。

    UIBarButtonItem *rightItem = [YBarButtonItem barButtonWithStyle:YBarButtonStyleRoundedRectangle  
                                                              Title:@"Save"  
                                                              Action:@selector(saveButtonClicked)  
                                                            Delegate:self];  
      
    self.navigationItem.leftBarButtonItem = rightItem;   
    这里就有个方法可以进行全局或局部的修改:
    !!!---本人开始用的时候也觉得奇怪,appearance方法怎么可以设置让所有同一种控件都为同一种风格,只能说是苹果太强大,他封装的了一个工具类UIAppearance可以统一设置控件的风格,当调用appearance方法时他会拿到相对应的控件(你用什么控件调用appearace方法,他就会返回什么控件),然后你可以设置统一主题(风格)---!!!
    iOS5提供了一个比较强大的工具UIAppearance,可以轻松的统一你的界面,它提供如下两个方法:
    + (id)appearance
    + (id)appearanceWhenContainedIn:(Class <>)ContainerClass,...
    第一个方法是统一全部改,比如你设置UINavigationBar 的tintColor,你可以这样写:
    [[UINavigationBar appearance] setTintColor:myColor];
    /**
       这样写之后,所有的 UINavigationBar 的tintColor的颜色都为myColor
    */
    第二个方法是当出现在某个类的出现时候才会改变:例如:
    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], [UIPopoverController class], nil] setTintColor:myPopoverNavBarColor];
     
    1.修改背景: 
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:leftButton  
                                                          forState:0  
                                                        barMetrics:UIBarMetricsDefault];  
     
    2.修改字体,阴影,字体颜色
    NSDictionary* textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:  
                                       BAR_BUTTON_TITLE_TEXT_COLOR,UITextAttributeTextColor,  
                                       BAR_BUTTON_TITLE_FONT,UITextAttributeFont,  
                                       BAR_BUTTON_TITLE_SHADOW_COLOR,UITextAttributeTextShadowColor,  
                                       [NSValue valueWithCGSize:CGSizeMake(1, 1)],UITextAttributeTextShadowOffset,  
                                       nil];  
      
       [[UIBarButtonItem appearance] setTitleTextAttributes:textAttributes forState:0];  
    3,修改UIBarButtonItem中文字的位置:
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(2, -1)  
                                                           forBarMetrics:UIBarMetricsDefault];  
  • 相关阅读:
    iOS学习笔记10改用一些更新的API smallelephant_A
    iOS学习笔记2微博cell界面的实现 smallelephant_A
    iOS学习笔记12UISearchBar smallelephant_A
    iOS学习笔记8地图开发 smallelephant_A
    iOS学习笔记5GCD smallelephant_A
    ios学习笔记之1 通讯录应用练习 smallelephant_A
    iOS学习笔记11沙盒 smallelephant_A
    iOS学习笔记13字典数据写入plist smallelephant_A
    iOS学习笔记14sqlite数据库初探 smallelephant_A
    iOS学习笔记7NSURLSession smallelephant_A
  • 原文地址:https://www.cnblogs.com/Fc-ios/p/3794163.html
Copyright © 2011-2022 走看看