IBInspectable的情节提要中的按钮上未显示角半径

IBInspectable的情节提要中的按钮上未显示角半径

本文介绍了使用@ IBDesigable/@ IBInspectable的情节提要中的按钮上未显示角半径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于按钮的自定义类.

I have this custom class for a button.

import UIKit

@IBDesignable
class CustomButton: UIButton {

    @IBInspectable var cornerRadiusValue: CGFloat = 10.0 {
        didSet {
            setUpView()
        }
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.layer.cornerRadius = 10.0
    }

    override func awakeFromNib() {
        setUpView()
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        setUpView()
    }

    func setUpView() {
        self.layer.cornerRadius = 10.0
    }

}

但情节提要中的按钮上未显示拐角半径.

But the corner radius is not showing on the button in the storyboard.

我了解@IBInspectable仅允许您更改检查器面板中的值.我想那不是我想要的.

I understand @IBInspectable just allows you to change the value in the inspector panel. I guess thats not what I am looking for.

当我使用该类创建按钮时,我希望拐角半径仅显示在情节提要中.我认为这就是@IBDesignable所做的.

I would like the corner radius to just show in storyboard when I create a button with that class. Which I thought that's what @IBDesignable does.

推荐答案

您的代码在IB中崩溃,因此可设计性失败.这是更简单的代码:

Your code is crashing in IB, so designability fails. Here is much simpler code that works:

@IBDesignable
class CustomButton: UIButton {
    @IBInspectable var cornerRadiusValue: CGFloat = 10.0 {
        didSet {
            setUpView()
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        setUpView()
    }
    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        setUpView()
    }
    func setUpView() {
        self.layer.cornerRadius = self.cornerRadiusValue
        self.clipsToBounds = true
    }
}

现在该按钮既可检查又可设计:

Now the button is both inspectable and designable:

它也可以在正在运行的应用程序中使用.

And it also works in the running app.

这篇关于使用@ IBDesigable/@ IBInspectable的情节提要中的按钮上未显示角半径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:25