第一种方法:
- (void)createTopView {
self.navigationController.navigationBarHidden = YES;
// 创建一个导航栏
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 375/WI * WIDTH, 100/HI * HEIGHT)];
// UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) ];
// 导航栏背景
[navBar setBackgroundImage:[UIImage imageNamed:@"bg_title"] forBarMetrics:UIBarMetricsDefault];
// [navBar setContentMode:UIViewContentModeScaleToFill];
// 自定义导航栏的title,用UILabel实现
// UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30/WI * WIDTH, 50/HI * HEIGHT)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 50/HI * HEIGHT)];
titleLabel.text = @"User Register\n用户注册";
titleLabel.textColor = [UIColor whiteColor];
titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
titleLabel.numberOfLines = 0;//表示label可以多行显示
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.backgroundColor = [UIColor redColor];
// 创建导航栏组件
UINavigationItem *navItem = [[UINavigationItem alloc] init];
// 设置自定义的title
navItem.titleView = titleLabel;
[navItem.titleView setContentMode:UIViewContentModeBottom];
// 创建左侧按钮背景图
UIImage *backButtonImage = [[UIImage imageNamed:@"btn_back_title"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//这里重新设置新的返回按钮
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStyleDone target:self action:@selector(onTapBack)];
//设置返回按钮的属性
UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSeperator.width = -15;//此处修改到左边界的距离
//将返回按钮和对应属性设置到导航栏
[navItem setLeftBarButtonItems:@[negativeSeperator, backBarButtonItem]];
//设置title高度
[navBar setTitleVerticalPositionAdjustment : -15/HI * HEIGHT forBarMetrics : UIBarMetricsDefault ];
[backBarButtonItem setBackgroundVerticalPositionAdjustment : -10.0 forBarMetrics : UIBarMetricsDefault ] ;
// 把导航栏组件加入导航栏
[navBar pushNavigationItem:navItem animated:false];
// 导航栏添加到view上
[self.view addSubview:navBar];
}
第二种方法:
- (void)createTopView {
self.navigationController.navigationBarHidden = YES;
// 导航栏背景图
UIImage *topImg = [UIImage imageNamed:@"title_lost_pwd"];
// 自定义导航栏
UIImageView *topView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, topImg.size.height)];
topView.userInteractionEnabled = YES;
topView.image = topImg;
topView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:topView];
// 返回按钮
UIImage *btnImg = [UIImage imageNamed:@"btn_back_title"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 20, topImg.size.height - 20, topImg.size.height - 20);
[backButton setBackgroundImage:btnImg forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(onBack) forControlEvents:UIControlEventTouchUpInside];
[topView addSubview:backButton];
}
- (void)onBack {
[self.navigationController popViewControllerAnimated:YES];
}