Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios swift是否可以更改(配置)interface builder上许多状态的按钮背景_Ios_Swift - Fatal编程技术网

Ios swift是否可以更改(配置)interface builder上许多状态的按钮背景

Ios swift是否可以更改(配置)interface builder上许多状态的按钮背景,ios,swift,Ios,Swift,我使用如下代码更改每个按钮的文本颜色: thirdTimeOption.titleLabel?.font = UIFont(name: Constants.centuryGothic.rawValue, size: CGFloat(13)) thirdTimeOption.setTitleColor(UIColor(red: 166.0/255.0, green: 142.0/255.0, blue: 83.0/255.0, alpha: 1.0), forState: UIControlSta

我使用如下代码更改每个按钮的文本颜色:

thirdTimeOption.titleLabel?.font = UIFont(name: Constants.centuryGothic.rawValue, size: CGFloat(13))
thirdTimeOption.setTitleColor(UIColor(red: 166.0/255.0, green: 142.0/255.0, blue: 83.0/255.0, alpha: 1.0), forState: UIControlState.Normal)
thirdTimeOption.setTitleColor(UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0), forState: UIControlState.Selected)
它是好的,它工作良好,但是,现在我还想改变每个按钮的背景,如果它被选中或未被选中。我没有找到任何函数来根据状态设置和配置背景颜色,我想我找到的按钮颜色也是一样

我在网上阅读,他们说我可以在interface builder上完成,但我不知道,我搜索了,但找不到

用户建议这样做: 如果选择了sender{

        sender.backgroundColor = UIColor(red: 166.0/255.0, green: 142.0/255.0, blue: 83.0/255.0, alpha: 1.0)
}else {
    sender.backgroundColor = UIColor.whiteColor()
}
但你看我加上这句话时得到了什么:


然而,我想要的只是将蓝色更改为我的颜色。我为您提供了一个解决方案。对UIImage类进行扩展并编写此代码

import Foundation
import UIKit
extension UIImage{
    static func imageFromColor(color: UIColor) -> UIImage {
    let rect = CGRectMake(0, 0, 1, 1);

    // create a 1 by 1 pixel context
    UIGraphicsBeginImageContextWithOptions(rect.size, false, 0);
    color.setFill()
    UIRectFill(rect);

    let image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;

  }
}
现在,您可以在ViewController类中使用它

let normalImageColor = UIImage.imageFromColor(UIColor.redColor())
let selectedImageColor = UIImage.imageFromColor(UIColor.greenColor())

//Use this with buttons
myButton.setBackgroundImage(normalImageColor, forState: .Normal)
myButton.setBackgroundImage(selectedImageColor, forState: .Selected)
你就完了。

替代方法

if(button.selected){
    button.backgroundColor = UIColor.redColor()
}
else{
    button.backgroundColor = UIColor.greenColor()
}
对于突出显示的状态,可以使用

if(button.highlighted){
    button.backgroundColor = UIColor.grayColor()
}
办理登机手续

@IBAction func buttonAction(sender: UIButton) {
    if(sender.selected){
        //Change colors
    }
}

我自己找到了解决办法

 @IBAction func selectTimeOption(sender: UIButton) {
        sender.selected = !sender.selected
        if sender.selected{
            sender.tintColor = UIColor(red: 166.0/255.0, green: 142.0/255.0, blue: 83.0/255.0, alpha: 1.0)
        }else {
            sender.tintColor = UIColor.whiteColor()
        }
    }

背景是背景图像。有一个函数可以根据状态设置背景图像。为什么不使用它呢?@matt我不想将背景作为图像,我需要的颜色只是颜色,为了保持圆角按钮,我知道我也可以通过代码进行圆角,但我试着问我是否可以不更改背景的颜色作为图像。背景图像可以很容易地由纯色组成。这与按钮的圆度无关。您没有显示使用按钮的其他操作(您的代码仅显示一些标题颜色)所以很难帮助你。如果你需要帮助,就要更加开放。是的,这是可以做到的。通过制作一个充满颜色的图像。但是@matt作者也要求它应该在界面生成器中完成。@RajanMaheshwari好吧,那太糟糕了。我专注于能做的事情。这比用头撞能做的事情更有效率不行。我知道我可以这样做,但每当按钮点击时我都必须这样做,但我要求的是不同的思考,我只需要说一次,我对文本颜色的思考是相同的。对于背景颜色,你可以在按钮的
i操作
中进行,在该操作中,你可以通过检查发件人是否选中或高来分配颜色亮了。我没法告诉你你想做什么?是的,我可以在ibaction上做,但是有没有办法只设置一次?(比如文本的颜色)我只在视图加载时设置了一次。但是您看到,每次iAction工作时,代码都会执行。如果您告诉我没有其他方法,我会在iAction中执行。我只是想知道可能的解决方案。据我所知,对于背景色,我们不能这样做。