【问题标题】:Not changing text color of buttons不改变按钮的文本颜色
【发布时间】:2017-07-31 10:26:12
【问题描述】:

当我单击 UIButton 时,我希望它更改其文本颜色。此外,我在每个按钮下方设置了一个视图,该按钮的下边距为 7。我这样做是为了通过更改视图的背景颜色为单击的按钮提供突出显示的效果。单击按钮时没有任何反应。这里附上截图

我希望它在点击时完全像这样

- (IBAction)setMenubtn:(UIButton *)menubtn:(id)sender {
      [self.menubtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
      [self.contactsbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
      [self.favoritesbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
      [self.topView setBackgroundColor:[UIColor colorWithRed:2/255 green:115/255 blue:168/255 alpha:1]];
      [self.topview2 setBackgroundColor:[UIColor colorWithRed:226/255 green:226/255 blue:226/255 alpha:1]];
      [self.topview3 setBackgroundColor:[UIColor colorWithRed:226/255 green:226/255 blue:226/255 alpha:1]];
   }

   - (IBAction)setContactsbtn:(UIButton *)contactsbtn:(id)sender {
       [self.menubtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
       [self.contactsbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
       [self.favoritesbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
       [self.topView setBackgroundColor:[UIColor colorWithRed:226/255 green:226/255 blue:226/255 alpha:1]];
       [self.topview2 setBackgroundColor:[UIColor colorWithRed:2/255 green:115/255 blue:168/255 alpha:1]];
       [self.topview3 setBackgroundColor:[UIColor colorWithRed:226/255 green:226/255 blue:226/255 alpha:1]];
  }

  - (IBAction)setFavoritesbtn:(UIButton *)favoritesbtn:(id)sender {
       [self.menubtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
       [self.contactsbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
       [self.favoritesbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
       [self.topView setBackgroundColor:[UIColor colorWithRed:226/255 green:226/255 blue:226/255 alpha:1]];
       [self.topview2 setBackgroundColor:[UIColor colorWithRed:226/255 green:226/255 blue:226/255 alpha:1]];
       [self.topview3 setBackgroundColor:[UIColor colorWithRed:2/255 green:115/255 blue:226/255 alpha:1]];
   }

它正在显示这个输出

【问题讨论】:

  • 尝试添加 .f 作为后缀。喜欢- [UIColor colorWithRed:226.f/255.f green:226.f/255.f blue:226.f/255.f alpha:1];
  • 这里sender有什么用
  • 这是正确的(IBAction)setMenubtn:(UIButton *)menubtn:(id)sender 否则
  • 应该是 --> (IBAction)setMenubtn:(UIButton *)menubtn
  • @Anbu.Karthik 什么都没发生

标签: ios objective-c uibutton


【解决方案1】:
  • 首先检查您的 Button 是否为 IBOutlets 并真正连接到 Storyboard 中的 Button。

如果按钮不是 IBOutlets 或连接到情节提要中的按钮,您的代码将无法更改属性,例如 self.menubtn。

  • 确保 IBOutlets 只连接一次。

  • 确保 IBAction 仅连接一次,并连接到正确的按钮,例如 touchUpInside

【讨论】:

    【解决方案2】:

    请尝试使用此代码向您展示如何在点击时更改 UIButton 颜色:

    button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setSelected:YES];
        button.frame = CGRectMake(x, y, width, height);
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
        [button setTitle:@"Button Title" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateHighlighted];
    

    【讨论】:

    • 这只是为了演示
    • 当x,y,width,height和图形不可用时会演示什么
    【解决方案3】:

    为什么要做硬编码。在 Github 中已经做了这种类型的自定义控件。 您可以根据需要自定义布局,并且很容易实现。

    HMSegmentedControl

    【讨论】:

    • 第三方资源不是所要求的。您可以在评论(我做过)中推荐它们,但不要将它们作为答案发布。第三方资源推荐问题是题外话
    猜你喜欢
    • 1970-01-01
    • 2016-11-13
    • 2013-09-24
    • 1970-01-01
    • 2015-11-23
    • 2015-12-01
    • 1970-01-01
    • 2015-02-02
    相关资源
    最近更新 更多