【问题标题】:Change background of a uinavigation right button?更改导航右键的背景?
【发布时间】:2014-09-10 07:57:39
【问题描述】:

我成功地通过给定代码更改了右导航按钮的文本。但我也想改变它有一个 默认背景

我只是想显示一个透明背景的文本。

我也试过了

 UIBarButtonItem *rightBtn=[[UIBarButtonItem alloc] initWithTitle:@"MyTitle" style:UIBarButtonItemStyleDone target:self action:@selector(myMethod)];

rightBtn.tintColor =[UIColor clearColor];

但仍显示按钮背景。

有没有办法通过改变任何没有背景的不同风格来实现它。还是有其他简单的方法可以将按钮更改为没有透明背景的文本。

【问题讨论】:

    标签: ios uinavigationcontroller uinavigationitem


    【解决方案1】:

    你可以用这个:

     UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
        btn.backgroundColor=[UIColor clearColor];
        [btn setTitle:@"Title" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rightBtn=[[UIBarButtonItem alloc] initWithCustomView:btn];
    
    
        [self.navigationItem setRightBarButtonItem:rightBtn];
    

    【讨论】:

      【解决方案2】:

      在 viewDidLoad 中添加这个:

      [self.navigationItem.rightBarButtonItem setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
      

      【讨论】:

      • 你能告诉我你在 viewDidLoad: 中的代码吗,包括 UIBarButtonItem 的创建。 (这在当前项目中对我有用)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多