您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 導覽欄:系統樣式與自訂樣式的實作

編輯:iOS 開發綜合

導覽欄有兩條路可走:一是沿用系統的 UINavigationBar,透過 navigationItem 去配置左右按鈕與標題;二是完全自繪一個 UIView 當作導覽欄。以下分別示範。

系統方式

用系統導覽欄時,先設定背景與陰影,再把自訂的 UIButton 包裝成 UIBarButtonItem 分別掛到左右兩側,中間標題則用 titleView 換成自訂的 UILabel,最後補上分隔線。

//1.設置導航欄背景圖片
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [[UIImage alloc]init];
[[self navigationController] setNavigationBarHidden:NO animated:YES];
self.navigationController.navigationBar.backgroundColor = [[UIColor alloc] initWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1.0];

//2.導航面板左邊的取消按鈕
UIButton* cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
if(cancelButton != nil) {
    [cancelButton setTitle:POST_CANCEL_BUTTON forState: UIControlStateNormal];
    [cancelButton setFrame:CGRectMake(0, 0, WIDTH_SCREEN/5.0, 44)];
    [cancelButton setTitleColor:[[UIColor alloc] initWithRed:0 green:158/255.0 blue:150/255.0 alpha:1.0]forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize: 16.0];
    cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [cancelButton addTarget:self action:@selector(cancelButtonEventTouchUpInside) forControlEvents :UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
    if(leftItem != nil) {
        self.navigationItem.leftBarButtonItem = leftItem;
    }
}

//3.導航面板右邊的發布按鈕
UIButton* postButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
if (postButton != nil) {
    [postButton setFrame:CGRectMake(0, 0, WIDTH_SCREEN/5.0, 44)];
    [postButton setTitle:@"發布" forState:UIControlStateNormal];
    [postButton setTitleColor:[[UIColor alloc] initWithRed:0 green:158/255.0 blue:150/255.0 alpha:1.0]forState:UIControlStateNormal];
    postButton.titleLabel.font = [UIFont systemFontOfSize: 16.0];
    postButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [postButton addTarget:self action:@selector(postButtonEventTouchUpInside) forControlEvents :UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:postButton];
    if(rightItem != nil) {
        self.navigationItem.rightBarButtonItem = rightItem;
    }
}

//4.導航面板中部文字
UILabel* navigationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
if (navigationLabel != nil) {
    [navigationLabel setTextColor:[UIColor blackColor]];
    navigationLabel.text = POST_NAVIGATION_TITLE;
    [navigationLabel setTextAlignment:NSTextAlignmentCenter];
    navigationLabel.font = [UIFont systemFontOfSize:18.0];
    self.navigationItem.titleView = navigationLabel;
}

//5.導航下面的一條分割線
UIView* line = [[UIView alloc]initWithFrame:CGRectMake(0, 20 + 44,WIDTH_SCREEN, 1)];
if (line != nil) {
    line.backgroundColor = [[UIColor alloc] initWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1.0];
    [self.view addSubview:line];
}

自訂方式

自訂做法則是把系統導覽欄隱藏起來,自己開一個 UIView 當容器,把左右按鈕、標題與分隔線全部加上去,位置完全自己說了算。

//1.創建導航欄視圖
UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN, 65)];
if (navView != nil) {
    //1.為導航視圖設置背景
    navView.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:248 / 255.0 blue:248 / 255.0 alpha:1];
    [[self navigationController] setNavigationBarHidden:YES animated:YES];

    //2.導航面板左邊的取消按鈕
    UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    if (leftButton != nil) {
        leftButton.frame = CGRectMake(15, 20, 65, 44);
        [leftButton setTitle:POST_CANCEL_BUTTON forState: UIControlStateNormal];
        [leftButton setTitleColor:[[UIColor alloc] initWithRed:0 green:158/255.0 blue:150/255.0 alpha:1.0]forState:UIControlStateNormal];
        leftButton.titleLabel.font = [UIFont systemFontOfSize: 16.0];
        leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [leftButton addTarget:self action:@selector(cancelButtonEventTouchUpInside) forControlEvents :UIControlEventTouchUpInside];
        [navView addSubview:leftButton];
    }

    //3.導航面板右邊的發布按鈕
    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    if (rightButton != nil) {
        rightButton.frame = CGRectMake(WIDTH_SCREEN - 80, 20, 65, 44);
        [rightButton setTitle:@"發布" forState:UIControlStateNormal];
        [rightButton setTitleColor:[[UIColor alloc] initWithRed:0 green:158/255.0 blue:150/255.0 alpha:1.0]forState:UIControlStateNormal];
        rightButton.titleLabel.font = [UIFont systemFontOfSize: 16.0];
        rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [rightButton addTarget:self action:@selector(postButtonEventTouchUpInside) forControlEvents :UIControlEventTouchUpInside];
        [navView addSubview:rightButton];
    }

    //4.導航面板中部文字
    UILabel* navTitle = [[UILabel alloc] initWithFrame:CGRectMake(80, 20, WIDTH_SCREEN - 80 - 80, 44)];
    if (navTitle != nil) {
        [navTitle setTextColor:[UIColor blackColor]];
        navTitle.text = POST_NAVIGATION_TITLE;
        [navTitle setTextAlignment:NSTextAlignmentCenter];
        navTitle.font = [UIFont systemFontOfSize:18.0];
        [navView addSubview:navTitle];
    }

    //5.在導航視圖底添加分割線
    UIView *navDividingLine = [[UIView alloc] init];
    if (navDividingLine != nil) {
        navDividingLine.frame = CGRectMake(0, 20 + 44, WIDTH_SCREEN, 1);
        navDividingLine.backgroundColor = [UIColor colorWithRed:221 / 255.0 green:221 / 255.0 blue:221 / 255.0 alpha:1];
        [navView addSubview:navDividingLine];
    }

    //6.往 view 增加導航欄
    [self.view addSubview:navView];
}

兩種做法各有取捨:系統方式省下版面計算、能自動配合狀態列與轉向;自訂方式則在視覺細節上自由度最高,代價是旋轉與安全區域都得自己處理。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved