定制 UIBarButtonItem:

 


 navigationController 内部有一个栈的实现,管理所有的view , 栈的底部是 根视图, 栈的顶部是当前显示的视图。
A navigation controller object manages the currently displayed screens using the  navigation stack . At the bottom of this stack is the root view controller and at the top of the stack is the view controller currently being displayed.

如果要显示新的视图:[self.navigationController pushViewController:viewController animated:YES];

所有继承自 UIViewController 的类,都有 navigationController 和 navigationItem 的属性,如果该类被加入到一个navigationController  的栈中,它的属性就指向 这个navigationController 。

navigationBar 一般有 左右两个按钮:
左侧按钮是 回退 或 取消:

The bar button item on the left side of the navigation bar allows for navigation back to the previous view controller on the navigation stack. The navigation controller updates the left side of the navigation bar as follows:

  • If the new top-level view controller has a custom left bar button item, that item is displayed. To specify a custom left bar button item, set the leftBarButtonItem property of the view controller’s navigation item.

  • If the top-level view controller does not have a custom left bar button item, but the navigation item of the previous view controller has a valid item in its backBarButtonItem property, the navigation bar displays that item.

  • If a custom bar button item is not specified by either of the view controllers, a default back button is used and its title is set to the value of the  title property of the previous view controller—that is, the view controller one level down on the stack. (If there is only one view controller on the navigation stack, no back button is displayed.)

    如果 当前的 navigationItem 手动设置了   leftBarButtonItem  参数,那么显示这个 buttonItem
    否则, 如果上一个 视图 手动设置了   backBarButtonItem 参数,那么显示这个buttonItem,
    如果没有 手动设置,那么显示默认的回退按钮,title 就是上个 视图的title , 指向上个 视图,就是栈的下一级。

    如果当前视图是 rootView ,可能需要设置 取消 按钮。


    navigationBar 右侧按钮一般是手动定制的。
    • If the new top-level view controller has a custom right bar button item, that item is displayed. To specify a custom right bar button item, set the rightBarButtonItem property of the view controller’s navigation item.

    • If no custom right bar button item is specified, the navigation bar displays nothing on the right side of the bar.

最后 BarButtonItem   ios 5 有 setBackButtonBackgroundImage:forState:barMetrics: 方法可指定背景,旧的版本只能通过 initWithCustomVIew 指定一个定制的UIButton 来实现。
http://stackoverflow.com/questions/7066874/customization-of-uinavigationbar-and-the-back-button  这里有完备的实例,通过category 方式封装的。

注意:
When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item. When this property is nil, the navigation item uses the value in its titleproperty to create an appropriate back button. If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead. When configuring your bar button item, do not assign a custom view to it; the navigation item ignores custom views in the back bar button anyway.

如果用initWithCustomVIew  的方式定制的  BarButtonItem  , 设置 backBarButtonItem 是不起作用的


注意2:
定制 UIbuttion 可拉伸背景图片,ios 4.3 是 UIImage stretchableImageWithLeftCapWidth:topCapHeight,ios 5后是  UIImage  resizableImageWithCapInsets
Return Value:A new image object with the specified cap insets.
他们返回的是一个新的 UIImage 对象,而不是改变现有的对象


你可能感兴趣的:(ios,NavigationBar)