如何在IOS项目中使用Navigation Bar(2)

主页我们需要一个帮助的按钮,这个按钮放在navigation bar的右边,上代码。

#pragma init bar button
-(void)initBarButton{
    //init a view
    UIView *rightButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 0, 50, 50)];
    //add the button to the view
    [rightButtonView addSubview:rightButton];
    [rightButton setImage:[UIImage imageNamed:@"help"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(help) forControlEvents:UIControlEventTouchUpInside];
    //set the view to the rightBarButtonItem
    UIBarButtonItem *rightCunstomButtonView = [[UIBarButtonItem alloc] initWithCustomView:rightButtonView];
    self.navigationItem.rightBarButtonItem = rightCunstomButtonView;
}

运行我们的代码 看看效果。


7798479-ded15125b13e4110.png
rightBt.png

添加完按钮之后 我们加个响应,在点击按钮后跳转到帮助页面。

#pragma help action
-(void)help{
    HelpVC* helpVc=[self.storyboard instantiateViewControllerWithIdentifier:@"HelpVC"];
    helpVc.view.backgroundColor=[UIColor whiteColor];
    [self.navigationController pushViewController:helpVc animated:YES];
}

当然在此之前我们要创建HelpVC,需要注意图上框住部分。

7798479-55d46aac3e29e5d8.png
helpVc.png

好的,运行代码看看效果。


7798479-a1428dae892b7568.png
helpView.png

因为我的HelpVc改为了HelpeVC所以图片上的HelpVc其实应该都是HelpVC.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值