navigationBarHidden和navigationBar.hidden的區別
? ? ? ?navigationBarHidden在官方文檔上的解釋是A Boolean value that indicates whether the navigation bar is hidden
navigationBar在官方文檔上的解釋是It is permissible to customize the appearance of the navigation bar using the methods and properties of the UINavigationBar class but you must never change its frame, bounds, or alpha values or modify its view hierarchy directly. To show or hide the navigation bar, you should always do so through the navigation controller by changing its navigationBarHidden property or calling the setNavigationBarHidden:animated: method.
意思就是說它可以通過UINavigationBar這個類的屬性來自定義的顯示navigation bar,但是你不能改變他的坐標,大小,透明度或者改變它的等級.為了展示或者隱藏navigation bar,你應該通過改變navigationBarHidden屬性或者調取setNavigationBarHidden:animated:這個方法.
設置導航欄的背景顏色
在iOS 7中,不再使用tintColor屬性來設置導航欄的顏色,而是使用barTintColor屬性來修改背景色。我們可以在AppDelegate.m文件中的方法didFinishLaunchingWithOptions:里面添加如下代碼來修改顏色:
[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];
默認情況下,導航欄的translucent屬性為YES,但是在一般的開發過程中設置為NO
[[UINavigationBar appearance] setTranslucent:NO];
在導航欄中使用背景圖片
如果希望在導航欄中使用一個圖片當做背景,那么你需要提供一個稍微高一點的圖片(這樣可以延伸到導航欄背后)。導航欄的高度從44 points(88 pixels)變為了64 points(128 pixels)。我們依然可以使用setBackgroundImage:方法為導航欄設置自定義圖片。如下代碼所示:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];
定制返回按鈕的顏色
要想給返回按鈕著色,可以使用tintColor屬性
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
如果想要用自己的圖片替換V型,可以設置圖片的backIndicatorImage和backIndicatorTransitionMaskImage。如下代碼所示:
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back_btn.png"]];
[[UINavigationBar?appearance]?setBackIndicatorTransitionMaskImage:[UIImage?imageNamed:@"back_btn.png"]];
修改導航欄標題的字體
跟iOS 6一樣,我們可以使用導航欄的titleTextAttributes屬性來定制導航欄的文字風格。在text attributes字典中使用如下一些key,可以指定字體、文字顏色、文字陰影色以及文字陰影偏移量:
UITextAttributeFont – 字體key
UITextAttributeTextColor – 文字顏色key
UITextAttributeTextShadowColor – 文字陰影色key
UITextAttributeTextShadowOffset – 文字陰影偏移量key
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor?=?[UIColor?colorWithRed:0.0?green:0.0?blue:0.0?alpha:0.8];
shadow.shadowOffset?=?CGSizeMake(0,?1);
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor?colorWithRed:245.0/255.0?green:245.0/255.0?blue:245.0/255.0?alpha:1.0],?NSForegroundColorAttributeName,
shadow,?NSShadowAttributeName,
[UIFont?fontWithName:@"HelveticaNeue-CondensedBlack"size:21.0],?NSFontAttributeName,?nil]];
修改電池電量條的風格
第一種方法:在iOS 7中,我們可以在每個view controller中overridingpreferredStatusBarStyle
-(UIStatusBarStyle)preferredStatusBarStyle
{
returnUIStatusBarStyleLightContent;
}
第二種:在UIApplication的statusBarStyle方法來設置狀態欄,不過,首先需要停止使用View controller-based status bar appearance。在project target的Info tab中,插入一個新的key,名字為View controller-based status bar appearance,并將其值設置為NO
然后就可以使用下面的代碼來設置狀態欄風格
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
隱藏狀態欄
- (BOOL)prefersStatusBarHidden
{
returnYES;
}