导航栏设置多个按钮、背景图、图片与文字按钮方法小结

设置一个按钮的方法就不再赘述了

  1. 设置多个按钮

法一:

    UIBarButtonItem *oneButton = [[UIBarButtonItemalloc]initWithTitle:@"设置" style:UIBarButtonItemStylePlain

target:selfaction:@selector(oneButtonAction:)];

    UIBarButtonItem *twoButton = [[UIBarButtonItemalloc]initWithTitle:@"消息" style:UIBarButtonItemStyleDone target:selfaction:@selector(twoButtonAction:)];

    NSArray *actionButtonItems = @[oneButton,   twoButton];

    self.navigationItem.rightBarButtonItems = actionButtonItems;

注:以上的style参数,我写的是UIBarButtonItemStylePlain和UIBarButtonItemStyleDone两种方式,UIBarButtonItemStyleDone默认字体为系统按钮的字体的设置(加粗),UIBarButtonItemStylePlain默认字体为lable的字体设置(17号字,无加粗)

 

法二:(此法本人不推荐,因为在点击时,两个按钮的背景(即UIToolbar的背景)都会有常亮显示)

    UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, WIDTHINIPHONE6(95), 44)];

    [tools setBarTintColor:DOMINANTCOLOR];

    [tools setTintColor:WHITEBACK];

    [tools setAlpha:[self.navigationController.navigationBar alpha]];

    UIBarButtonItem *setBtn = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:self action:@selector(setUserSetting)];

    UIBarButtonItem *msgBtn = [[UIBarButtonItem alloc] initWithTitle:@"消息" style:UIBarButtonItemStylePlain target:self action:@selector(lookUserMsg)];

    NSArray *btnArr = @[setBtn,msgBtn];

    [tools setItems:btnArr];

    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:tools];

 

      2.  背景图

 [navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];

      3.  图片与文字按钮(这里我以自定义左部按钮为例)

    _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftBtn.frame = CGRectMake(0, 0, 60, 25);
    [_leftBtn setTitle:@"返回" forState:UIControlStateNormal];
    [_leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    _leftBtn.titleLabel.font = HEADFONT;
    [_leftBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
    _leftBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    _leftBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:_leftBtn];
    self.navigationItem.leftBarButtonItem = leftButton;

 

参考:http://blog.csdn.net/lengshengren/article/details/16820803

转载于:https://www.cnblogs.com/linzhengbo/p/5657748.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
设置小程序导航栏右侧按钮的背景色,可以使用`setNavigationBarColor` API接口来实现。具体步骤如下: 1. 在页面的`onLoad`生命周期函数中调用`wx.setNavigationBarColor`方法设置导航栏颜色。示例代码如下: ```javascript Page({ onLoad: function () { wx.setNavigationBarColor({ frontColor: '#ffffff', // 前景色 backgroundColor: '#ff0000', // 背景色 }) } }) ``` 2. 在小程序的`app.json`配置文件中,将`"navigationStyle"`属性设置为`"custom"`,即可禁用小程序默认的导航栏。示例代码如下: ```json { "pages": [ "pages/index/index" ], "window": { "navigationBarTitleText": "小程序标题", "navigationBarBackgroundColor": "#ffffff", "navigationBarTextStyle": "black", "backgroundTextStyle": "light" }, "navigationStyle": "custom" } ``` 3. 在页面的`wxml`文件中,使用`<view>`标签来实现自定义导航栏,将右侧按钮的代码放在`<view>`标签中,并设置背景色为红色。示例代码如下: ```html <view class="nav"> <view class="nav-title">小程序标题</view> <view class="nav-btn" style="background-color: #ff0000;"></view> </view> ``` 4. 在页面的`wxss`文件中,设置导航栏的样式,如导航栏高度、按钮样式等。示例代码如下: ```css .nav { display: flex; justify-content: space-between; align-items: center; height: 44px; padding: 0 10px; background-color: #ffffff; border-bottom: 1px solid #f0f0f0; } .nav-title { font-size: 16px; font-weight: bold; } .nav-btn { width: 44px; height: 44px; } ``` 通过以上步骤,即可实现小程序导航栏右侧按钮的背景色设置

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值