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

導覽列客製化:背景色、按鈕標題與圖片座標怎麼調

編輯:iOS 開發基礎

一、改造系統原生導覽列

改背景色

self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"#2295f2"];

自訂左右按鈕

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn-menu-h"]
                                                                          style:UIBarButtonItemStylePlain
                                                                         target:self
                                                                         action:@selector(menuBtnClick)];

按鈕圖片離螢幕邊緣太遠?調 imageInsets

self.navigationItem.leftBarButtonItem.imageInsets = UIEdgeInsetsMake(0, -20, 0, 0);
self.navigationItem.rightBarButtonItem.imageInsets = UIEdgeInsetsMake(0, -10, 0, 10);

設了背景色之後,按鈕標題顏色也會跟著變,用 tintColor 改回來

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

標題的顏色與字級

self.navigationController.navigationBar.titleTextAttributes = @{
    NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#ffffff"],
    NSFontAttributeName : [UIFont systemFontOfSize:15]
};

按鈕標題的顏色與字級

[self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{
    NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#ffffff"],
    NSFontAttributeName : navItermFont
} forState:UIControlStateNormal];

別忘了半透明造成的色差

導覽列預設是半透明的,若不處理,實際呈現的顏色會和設計稿有落差。

關掉半透明

這行程式碼能關閉半透明效果,但副作用是原點座標會往下移。

[UINavigationBar appearance].translucent = NO;

順便修掉原點位移

self.edgesForExtendedLayout = UIRectEdgeNone;

關於 translucent 的更多細節,可以參考〈iOS 7 導覽列適配——translucent 屬性設置的問題〉。

二、完全自訂導覽列

當系統導覽列滿足不了設計稿時,就得自己畫一個。下面的程式碼只示範標題與左右按鈕,實際可依設計加入任意控件。

@interface CustomNav : UIView
@property (nonatomic, strong) UIButton *buttonLeft;
@property (nonatomic, strong) UIButton *buttonRight;
- (id)initWithTitle:(NSString *)title;
@end

@implementation CustomNav
- (id)initWithTitle:(NSString *)title {
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, 64);

        // 標題
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.frame = CGRectMake(SCREEN_WIDTH / 4.0, 22, SCREEN_WIDTH / 2.0, 40);
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
        titleLabel.font = [UIFont systemFontOfSize:17];
        titleLabel.text = title;
        [self addSubview:titleLabel];

        // 左按鈕
        self.buttonLeft = [UIButton buttonWithType:UIButtonTypeCustom];
        self.buttonLeft.frame = CGRectMake(0, 22, 40, 40);
        [self addSubview:self.buttonLeft];
        self.buttonLeft.titleLabel.font = [UIFont systemFontOfSize:15];
        self.buttonLeft.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;

        // 右按鈕
        self.buttonRight = [UIButton buttonWithType:UIButtonTypeCustom];
        self.buttonRight.frame = CGRectMake(SCREEN_WIDTH - 40, 22, 40, 40);
        [self addSubview:self.buttonRight];
        self.buttonRight.titleLabel.font = [UIFont systemFontOfSize:15];
        self.buttonRight.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

        self.backgroundColor = [UIColor colorWithHexString:@"#2295f2"];
    }
    return self;
}
@end
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved