简体   繁体   English

自定义按钮未在Interface Builder中正确显示

[英]Custom button is not showing correctly in Interface Builder

I'm learning to develop iOS apps in XCode. 我正在学习用XCode开发iOS应用。 I've created simple custom button (subclass of UIButton) that should have red background and green text. 我创建了简单的自定义按钮(UIButton的子类),该按钮应具有红色背景和绿色文本。 When running the app in iOS simulator, everything is ok. 在iOS模拟器中运行应用程序时,一切正常。 But in interface builder other colors (specified in interface builder) are used. 但是在界面构建器中,使用了其他颜色(在界面构建器中指定)。 Test project is available at https://www.dropbox.com/s/ro7i4omdbpwapi0/testapp%20%282%29.zip?dl=0 . 测试项目位于https://www.dropbox.com/s/ro7i4omdbpwapi0/testapp%20%282%29.zip?dl=0 Can somebody help? 有人可以帮忙吗?

Here is source code of custom UIbutton class: 这是自定义UIbutton类的源代码:

import UIKit

@IBDesignable class DefaultButton: UIButton {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }

    func setup() {
        setTitleColor(UIColor.red, for: .normal)
        backgroundColor = UIColor.green
        //setBackgroundImage(UIImage.createGradient(colors:  [ UIColor.defaultGradientStartColor, UIColor.defaultGradientEndColor ], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 1, y: 0), size: CGSize(width: 200, height: 200)), for: .normal)
        //layer.cornerRadius = 50
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        setup()
    }
}

The behaviour described in you answer is correct; 您回答中描述的行为是正确的; but i think you mixes two different things. 但是我认为您混合了两种不同的东西。

Basically, if you want to tint your button title, and your button background, you have to use the fields availables in interface builder. 基本上,如果要着色按钮标题和按钮背景,则必须使用界面构建器中的可用字段。 You have some nice properties to set up these things easely. 您有一些不错的属性可以轻松设置这些内容。

Use interface builder's parameters to design your look 使用界面生成器的参数设计外观

So, in this case, defining a titleColor and a backgroundColor in your view setup is not relevant, since it's hardcoded. 因此,在这种情况下,在视图设置中定义titleColorbackgroundColor 无关紧要 ,因为它们是硬编码的。

If instead, you want to display a custom look to your button - like rounded corner - in your storyboard, you could use the @IBInspectable keyword to add new properties in interface builder. 如果相反,您希望在故事板上显示按钮的自定义外观(如圆角),则可以使用@IBInspectable关键字在界面构建器中添加新属性。

@IBDesignable
class DefaultButton: UIButton {

    @IBInspectable var borderWidth: CGFloat = 0 {
        didSet {
            layer.borderWidth = borderWidth
        }
    }
    @IBInspectable var borderColor: UIColor? {
        didSet {
            layer.borderColor = borderColor?.cgColor
        }
    }
    @IBInspectable var cornerRadius: CGFloat = 0 {
        didSet {
            layer.cornerRadius = cornerRadius
            layer.masksToBounds = cornerRadius > 0
        }
    }

    // ...
}

By doing this, you can edit and see your view with a nice render in your storyboard! 这样,您便可以在情节提要中编辑并查看带有漂亮渲染的视图!

Custom properties directly in storyboard 直接在情节提要中自定义属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM