1. 程式人生 > >iOS與導航欄有關的神技

iOS與導航欄有關的神技

  1、導航欄上的返回按鈕

<span style="font-size:24px;">   </span><span style="font-size:18px;">self.navigationController.navigationBar.topItem.title=self.message;
   self.navigationController.navigationBar.tintColor=[UIColor blackColor];
   UIBarButtonItem*backButton = [[UIBarButtonItemalloc] initWithTitle:@"返回"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(PopViewController)];
   self.navigationItem.leftBarButtonItem= backButton;</span>

   2、導航欄上的按鈕自定義

    UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    editBtn.frame = CGRectMake(0, 0, 60, 40);
    [editBtn setImage:[UIImage imageNamed:@"nav_right_edit"] forState:UIControlStateNormal];
    [editBtn setTitle:@"完成" forState:UIControlStateSelected];
    [editBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [editBtn addTarget:self action:@selector(editButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    //建立navigationBar右側Item
    self.editButton = [[UIBarButtonItem alloc] initWithCustomView:editBtn];

  3、導航欄的一些設定

    UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
    navigationBarAppearance.barTintColor = [UIColor whiteColor];
    navigationBarAppearance.tintColor = [UIColor colorWithRed:0.098 green:0.098 blue:0.098 alpha:0.7];
    navigationBarAppearance.translucent = NO;
    //設定導航欄上標題的字型大小及顏色
    [navigationBarAppearance setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:20],
       NSForegroundColorAttributeName:[UIColor blackColor]}];
    //設定導航欄的背景圖片
//  [navigationBarAppearance setBackgroundImage:[UIImage imageNamed:@"header_background"] forBarMetrics:UIBarMetricsDefault];
    //設定導航欄的背景顏色
    [navigationBarAppearance setBackgroundColor:[UIColor whiteColor]];
    //自定義返回按鈕的圖片樣式
//  UIImage *backButtonImage = [[UIImage imageNamed:@"nav_back"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
//  [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    //設定返回按鈕標題的偏移 
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];

    4、透明設定和清除黑線

   //導航欄變為透明
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:0];
   //讓黑線消失的方法
    self.navigationController.navigationBar.shadowImage=[UIImage new];

   以上內容為自己在開發總結,希望可以幫助到有需要的人。如有遺漏或者不足之處,還請各位多加指教。也可以加入 QQ群:181798048 相互學習交流,iOS滬上程式碼小子書