自定義iOS導航欄背景,標題和返回按鈕文字顏色


一、導航欄的背景和文字Color:

方法一:

  1. //設置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],NSForegroundColorAttributeName,nil]];  

方法二:

  1. //設置NavigationBar背景顏色  
  2. [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];  
  3. //@{}代表Dictionary  
  4. [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];  

二、導航欄使用背景圖片:

1. [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];  

三、導航欄標題的字體:

UITextAttributeFont - 字體

UITextAttributeTextColor - 文字顏色

UITextAttributeTextShadowColor - 文字陰影顏色

UITextAttributeTextShadowOffset - 偏移用於文本陰影

  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(0, 1);  
  4. [[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,  
  5. shadow, NSShadowAttributeName,  
  6. [UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil nil]];  

四、圖片作為導航欄標題:

  1. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"appcoda-logo.png"]];  

五、添加多個欄按鈕項目:

  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; 

六、自定義后退按鈕的文字和顏色:

方法一:

  1. UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];  
  2. self.navigationItem.backBarButtonItem = item;  
  1. [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];  
  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(0, 30, 0, 0)];  
  7. [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];  
  8. //將返回按鈕的文字position設置不在屏幕上顯示  
  9. [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM