iOS 自定义导航栏

第一种方法:


- (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];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值