導航欄背景,標題.按鈕

在iOS7下,默認導航欄背景,顏色是這樣的,接下來咱們就進行自定義,若是你僅僅是更改一下背景和顏色,代碼會很簡單,不須要很複雜的自定義View來替代leftBarItem 網絡


更改導航欄的背景和文字Color

方法一: app

[objc]  view plain copy 在CODE上查看代碼片 派生到個人代碼片
  1. //set NavigationBar 背景顏色&title 顏色  
  2. [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:20/255.0 green:155/255.0 blue:213/255.0 alpha:1.0]];  
  3. [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];  
效果以下:

咱們把背景改爲了藍色,title文字改爲了白色,是否是很簡單呢?NavigationBar極其push過去的子頁面也會是你修改後的背景顏色 ide

方法二: 字體

[objc]  view plain copy 在CODE上查看代碼片 派生到個人代碼片
  1. //設置NavigationBar背景顏色  
  2. [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];  
  3. //@{}表明Dictionary  
  4. [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];  
效果以下:


在導航欄使用背景圖片:

若是您的應用程序使用了自定義圖像做爲欄的背景,你須要提供一個「更大」的圖片,使其延伸了狀態欄的後面。導航欄的高度如今是從44點(88像素)更改成64點(128像素)。
仍然可使用了setBackgroundImage:方法來指定自定義圖像的導航欄。下面是代碼行設置背景圖片: ui

[objc]  view plain copy 在CODE上查看代碼片 派生到個人代碼片
  1. [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];  

效果圖和上面的同樣,我就不貼出來了。 spa


改變導航欄標題的字體

就像iOS 6,咱們能夠經過使用導航欄的「titleTextAttributes」屬性來自定義的文本樣式。能夠指定字體,文字顏色,文字陰影顏色,文字陰影在文本標題偏移屬性字典,使用下面的文本屬性鍵: .net

UITextAttributeFont - 字體
UITextAttributeTextColor - 文字顏色
UITextAttributeTextShadowColor - 文字陰影顏色
UITextAttributeTextShadowOffset - 偏移用於文本陰影 code

[objc]  view plain copy 在CODE上查看代碼片 派生到個人代碼片
  1. NSShadow *shadow = [[NSShadow alloc] init];  
  2. shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];  
  3. shadow.shadowOffset = CGSizeMake(01);  
  4. [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:  
  5. [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,  
  6. shadow, NSShadowAttributeName,  
  7. [UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil nil]];  


使用圖片做爲導航欄標題

不想標題欄是光禿禿的文字?能夠經過使用代碼行中的圖像或標誌取代它:簡單地改變titleview用來自定義,(適用於較低版本) orm

[objc]  view plain copy 在CODE上查看代碼片 派生到個人代碼片
  1. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"appcoda-logo.png"]];  

效果以下,我隨便用了個圖片,別介意: blog

還有一個例子:


CGRect rect = [UIScreen mainScreen].bounds;

    _friendsCircleTitleView = [[UIView allocinitWithFrame:CGRectMake(00,14029)];

    _friendsCircleTitleView.backgroundColor = [UIColor clearColor];

    _friendsCircleTitleView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

    

    _friendsCircleTitleView.autoresizesSubviews = YES;

    

    

    _sameCityBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    _sameCityBtn.backgroundColor = [UIColor clearColor];

    _sameCityBtn.frame = CGRectMake(007029);

    [_sameCityBtn setTitle@"同城" forStateUIControlStateNormal];

    [_sameCityBtn setTitleColor: [UIColor whiteColorforStateUIControlStateNormal];

    [_sameCityBtn setTitleColor: [UIColor whiteColorforStateUIControlStateHighlighted];

    _sameCityBtn.titleLabel.font = [UIFont boldSystemFontOfSize13.0];

    [_sameCityBtn setBackgroundImage:[UIImage imageNamed:@"sameCity"forState:UIControlStateNormal];

    [_sameCityBtn setBackgroundImage:[UIImage imageNamed@"sameCitySelected"forState:UIControlStateHighlighted];

    [_sameCityBtn setBackgroundImage:[UIImage imageNamed@"sameCitySelected"forState:UIControlStateSelected];

    _sameCityBtn.tag = TAG_OF_SAMECITY_BTN;

    [_sameCityBtn addTarget:self action:@selector(onLeftTitleBtnPressed)forControlEvents:UIControlEventTouchUpInside];

    [_friendsCircleTitleView addSubview_sameCityBtn];

    _sameCityBtn.selected = YES;

    _sameCityBtn.userInteractionEnabled = NO;

    

    _wholeCountryBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    _wholeCountryBtn.backgroundColor = [UIColor clearColor];

    _wholeCountryBtn.frame = CGRectMake(_sameCityBtn.frame.origin.x+7007029);

    [_wholeCountryBtn setTitle:@"全國" forStateUIControlStateNormal];

    [_wholeCountryBtn setTitleColor: [UIColor whiteColorforStateUIControlStateNormal];

    [_wholeCountryBtn setTitleColor: [UIColor whiteColorforStateUIControlStateHighlighted];

    _wholeCountryBtn.titleLabel.font = [UIFont boldSystemFontOfSize:13.0f];

    [_wholeCountryBtn setBackgroundImage:[UIImage imageNamed@"wholeCountryNomal"]forState:UIControlStateNormal];

    [_wholeCountryBtn setBackgroundImage:[UIImage imageNamed@"wholeCountrySelected"]forStateUIControlStateHighlighted];

    [_wholeCountryBtn setBackgroundImage:[UIImage imageNamed@"wholeCountrySelected"]forStateUIControlStateSelected];

    _wholeCountryBtn.tag = TAG_OF_WHOLECOUNTY_BTN;

    [_wholeCountryBtn addTarget:self action:@selector(onRightTitleBtnPressed)forControlEvents:UIControlEventTouchUpInside];

    [_friendsCircleTitleView addSubview:_wholeCountryBtn];

    _wholeCountryBtn.selected = NO;

   

        self.navigationItem.titleView = _friendsCircleTitleView;

效果如圖:

添加多個欄按鈕項目

您但願添加導航欄的一側不止一個欄按鈕項目,不管是leftBarButtonItems和rightBarButtonItems 您在導航欄左側/右側指定自定義欄按鈕項目。好比你想添加一個攝像頭和一個共享按鈕右側的吧。您可使用下面的代碼:

[objc]  view plain copy 在CODE上查看代碼片 派生到個人代碼片
  1. UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action: nil nil];  
  2. UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action: nil nil];  
  3. NSArray *itemsArr = @[shareItem,cameraItem];  
  4. self.navigationItem.rightBarButtonItems = itemsArr;  


自定義後退按鈕的文字和顏色

一般狀況下,咱們使用UINavigationController時,push到的子頁面,左上角會是系統自動取值上一層父頁面的title名稱,默認狀況是這樣,那麼咱們該如何修改它呢?

左側顯示了父頁面的title:用戶登陸,但是咱們想修改爲返回,方式有不少,舉些例子

方法一:

經過設置navigationItem的backBarButtonItem能夠直接更換文字,【注意,要在父視圖的Controller中設置】以下:

[objc]  view plain copy 在CODE上查看代碼片 派生到個人代碼片
  1. UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];  
  2. self.navigationItem.backBarButtonItem = item;  
效果以下:

全部的子界面返回時都變成了咱們定義的文字,若是不想顯示文字,直接"",就會單獨顯示一個系統的返回箭頭圖標,也是很清晰的感受。


作到這裏發現文字顏色和背景有重複,那麼如何自定義其顏色呢?在iOS7,能夠改變tintColor屬性,它提供了一個快速和簡單的方式,下面是一個示例代碼片斷:

[objc]  view plain copy 在CODE上查看代碼片 派生到個人代碼片
  1. [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];  
效果以下:

全是系統的圖標和文字,這回看着舒服了,有木有?【除了後退按鈕,請注意,tintColor屬性影響全部按鈕標題和按鈕圖像


最後舉個例子,另一種實現自定義導航控制器返回按鈕,代碼以下:

[objc]  view plain copy 在CODE上查看代碼片 派生到個人代碼片
  1.  [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:19.0]}];  
  2.   
  3. self.title=[NSString stringWithFormat:@"第%lu頁",(unsigned long)self.navigationController.viewControllers.count];  
  4.   
  5. //自定義返回按鈕  
  6. UIImage *backButtonImage = [[UIImage imageNamed:@"fanhui.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(03000)];  
  7. [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];  
  8. //將返回按鈕的文字position設置不在屏幕上顯示  
  9. [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];  

效果以下:


還有一種方法


  UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 30, 30)];

    [leftBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];

    [leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];

    

    self.navigationItem.leftBarButtonItem = leftBarItem;

效果如圖:


最後說一下使用pushViewController切換到下一個視圖時,navigation controller按照如下3條順序更改導航欄的左側按鈕(本段摘自網絡):

一、若是B視圖有一個自定義的左側按鈕(leftBarButtonItem),則會顯示這個自定義按鈕;
二、若是B沒有自定義按鈕,可是A視圖的backBarButtonItem屬性有自定義項,則顯示這個自定義項;
三、若是前2條都沒有,則默認顯示一個後退按鈕,後退按鈕的標題是A視圖的標題;




如何去掉導航欄下面的那條黑線呢?

如圖

有條線很難看,在

didFinishLaunchingWithOptions中增長

 [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];

    [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

這兩行代碼就能夠了。

效果以下:

相關文章
相關標籤/搜索