自定义导航栏 标题视图 返回按钮

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    ////自定义标题视图
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 120, 44)];
    titleLabel.backgroundColor = [UIColor grayColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:19];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"新闻";
    self.navigationItem.titleView = titleLabel;
}

 

#pragma mark - 自定义返回按钮 
- (void)addBackButton {
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    [backButton addTarget:self action:@selector(onBack) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateNormal];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
    backButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 34);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
}

- (void)onBack {
    [self.navigationController popViewControllerAnimated:YES];
}

 

posted @ 2017-05-02 11:37  宁静暖风  阅读(185)  评论(0编辑  收藏  举报