【问题标题】:UIButton fixed background color when clicked in Objective C在Objective C中单击时UIButton固定背景颜色
【发布时间】:2016-04-10 05:06:05
【问题描述】:

我一直在尝试将按钮的背景颜色固定在 .xib 上。就像,默认背景是白色的,但是当单击按钮时,它将是蓝色或黑色,并且在单击另一个按钮之前它将保持这种状态。 我使用UIControlState、背景颜色和图像进行了大部分组合,但似乎没有任何效果。背景颜色改变一秒钟,并在几秒钟后恢复默认。

这是我尝试过的组合之一

[self.theButton addTarget:self action:@selector(slideAction:) forControlEvents:UIControlEventTouchUpInside];
-(void) slideAction:(UIButton *)myButton
{
    [self.theButton setImage:[UIImage imageNamed:@"blue.png"] forState:UIControlStateNormal];
}

功能有效,但不更新颜色。我不知道问题是否与.xib有关。 我很感激你可以帮助我解决这个问题。 谢谢。

【问题讨论】:

标签: ios objective-c xcode uibutton xib


【解决方案1】:

在该按钮上单击输入以下代码

-(void) slideAction:(UIButton *)myButton
{
    [self.theButton setBackgroundColor:[UIColor blueColor]];
}

然后将该代码放入另一个按钮单击

-(void) AnotherButtonAction:(UIButton *)myButton
    {
        [self.theButton setBackgroundColor:[UIColor whiteColor]];
    }

【讨论】:

    【解决方案2】:

    请按照这个方法:

    - (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state;
    

    【讨论】:

      【解决方案3】:
      Try below code
      
        -(void)code:(UIButton *)sender
          {
              for (UIView* subView in self.view.subviews)
              {
                  if ([subView isMemberOfClass:[UIButton class]])
                  {
                      [subView setBackgroundColor:[UIColor greenColor]];
                      [sender setBackgroundColor:[UIColor blackColor]];
                  }
              }
          }
      
          -(IBAction)firstBtnAction:(id)sender
          {
              [self code:sender];
          }
      
          -(IBAction)secondBtnAction:(id)sender
          {
              [self code:sender];
          }
      
          -(IBAction)thirdBtnAction:(id)sender
          {
              [self code:sender];
          }
      
          -(IBAction)fourthBtnAction:(id)sender
          {
              [self code:sender];
          }
      

      【讨论】:

        【解决方案4】:

        这是因为backgroundColorUIView 的属性,正如您所知,UIButtonUIView 的子类。

        这很重要,因为这样就更容易理解为什么这个属性没有状态:视图没有状态。出于某种原因,Apple 决定对按钮的属性行为进行点扩展。

        iOS中创建的根据State改变UIButtons的backgroundColor的方法是:setBackgroundImage: forState:。我知道,这意味着您需要每个州的图像。浪费空间和内存。

        为了解决这个问题,我创建了一个类别来处理带有UIButtonsbackgroundColor 状态:
        ButtonBackgroundColor-iOS

        您可以将类别安装为pod

        或者按照老派的方式进行:将文件夹/ButtonBackgroundColor-iOS 拖到您的项目中。就是这样。

        易于使用 Objective-C

        @property (nonatomic, strong) UIButton *myButton;
        
        ...
        
        [self.myButton bbc_backgroundColorNormal:[UIColor redColor]
                         backgroundColorSelected:[UIColor blueColor]];
        

        使用 Swift 更容易使用:

        import ButtonBackgroundColor
        
        ...
        
        let myButton:UIButton = UIButton(type:.Custom)
        
        myButton.bbc_backgroundColorNormal(UIColor.redColor(), backgroundColorSelected: UIColor.blueColor())
        

        我建议您使用以下方式导入 pod:

        platform :ios, '8.0'
        use_frameworks!
        
        pod 'ButtonBackgroundColor', '~> 1.0'
        

        使用 use_frameworks!在你的 Podfile 中使用 Swift 和 Objective-C 更容易使用你的 pod。

        重要

        I also wrote a Blog Post with more information.

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-10-19
          • 1970-01-01
          • 1970-01-01
          • 2011-09-29
          • 1970-01-01
          • 1970-01-01
          • 2016-04-07
          • 1970-01-01
          相关资源
          最近更新 更多