最近,看了標(biāo)哥的博客-《iOS導(dǎo)航標(biāo)題不居中問(wèn)題》(原文地址http://www.henishuo.com/ios-nav-center-question/)不甚理解貌笨,特看了下UINavigationBar的層級(jí)結(jié)構(gòu)
我沒(méi)有自定義左邊item,用的系統(tǒng)的item襟沮。打印了navigationBar的subviews锥惋,結(jié)果如下:
class:_UINavigationBarBackground: 0x7a8f7130; frame = (0 -20; 320 64); autoresize = W; userInteractionEnabled = NO; layer =>0x7a8f6830>>,,,superClass:_UIBarBackgroundImageView;
class:UINavigationItemView: 0x7a8f4450; frame = (106 8; 108 27); opaque = NO; userInteractionEnabled = NO;layer = <CALayer: 0x7a8f4500>>,,,,superClass:UIView;
class:UIButton: 0x7a7bbd70; frame = (6 7; 30 30); opaque = NO; layer =<CALayer:0x7a7bbe90>>,,,,superClass:UIControl;
class:<UIView: 0x7a7ba490; frame = (274 2; 40 40); layer =<CALayer: 0x7a7ba520>>,,,superClass:UIResponder
class:<_UINavigationBarBackIndicatorView: 0x7a839550; frame = (8 11.5; 13 21); alpha = 0; opaque = NO; userInteractionEnabled = NO; layer =<CALayer: 0x7a839520>>,,,superClass:UIImageView.
_UINavigationBarBackground就是整個(gè)NavigationBar
UINavigationItemView是前一個(gè)頁(yè)面的titleView
UIView是前一個(gè)頁(yè)面的右邊的item,
UIButton是返回按鈕开伏,設(shè)置[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];設(shè)置的是返回按鈕的背景圖膀跌,要設(shè)置偏移量,不然圖會(huì)被嚴(yán)重拉伸
_UINavigationBarBackIndicatorView是我們看到的那個(gè)返回剪頭圖片固灵,所以設(shè)置self.navigationBar.backIndicatorImage = image;是設(shè)置的不是返回按鈕的圖片
UIBarButtonItem會(huì)放在UINavigationItem中捅伤,UINavigationItem在UINavigationBar的items中。當(dāng)我們沒(méi)有設(shè)置backBarButtonItem時(shí)巫玻,UINavigationItem中的backBarButtonItem也為nil丛忆。
_UINavigationBarBackground與_UINavigationBarBackIndicatorView在navigation不斷push新controller過(guò)程中不會(huì)創(chuàng)建新對(duì)象,但UIButton在subviews中并沒(méi)有找到仍秤。并且backBarButtonItem與UIButton熄诡、_UINavigationBarBackIndicatorView的顯示關(guān)系還沒(méi)有找到,backButtonTitle相對(duì)應(yīng)的顯示關(guān)系也沒(méi)有找到诗力,等待繼續(xù)發(fā)掘凰浮。