【问题标题】:Custom Class View for UIButtonUIButton 的自定义类视图
【发布时间】:2016-08-16 09:41:05
【问题描述】:

在 Identity Inspector 中,我想为我的 UIButton 设置一个自定义类。 因此,我创建了 UIView 的自定义类,但我的自定义类没有出现在列表中。 反之亦然,如果我添加 UIView,列表上会出现大多数包含 UIButton 的类。

如果UIButtonUIView 的子类,而不是UIViewUIButton, 为什么有可能,为什么我不能使用我的 UIView 自定义类来代表我的 UIButton

【问题讨论】:

  • 我不建议子类化 UIButton,但如果你想这样做,创建继承自 UIButton 的 customClass,而不是 UIView。然后它会出现在身份检查器中

标签: ios objective-c iphone uiview uibutton


【解决方案1】:

我不太确定你的意思,但我会试一试。

你的问题...

如果 UIButton 是 UIView 的子类,而不是 UIView 是 UIButton,为什么它可能以及为什么我不能使用我的 UIView 自定义类来表示我的 UIButton。

我相信你在说...

  1. UIButtonUIView 的子类。
  2. 您的自定义视图是UIView 的子类。
  3. 为什么不能将 UIButton 设置为自定义视图?

是吗?

如果是这样,那就很清楚了。仅仅因为它们派生自同一个类并不意味着它们是相关的。 UILabel 也派生自 UIView,但非常不同。

如果您想创建UIButton 的自定义子类,那么您的自定义类应该派生自UIButton...

你应该...

@interface YourSubclass: UIButton

而不是...

@interface YourSubclass: UIView

这样做会使自定义子类出现在 Interface Builder 的 Identity Inspector 中。

【讨论】:

  • 好的,我明白了,在 UIView 列表中我显示 UIButton,因为它是 UIView 的子类。抱歉这个简单的问题,我是新手
【解决方案2】:

您可以为 UIButton 创建自定义子类。然后将其设置为任何按钮的自定义类。

class CCButton: UIButton {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        createBorder()
    }

    required override init(frame: CGRect) {
        super.init(frame: frame)
        createBorder()
    }

    private func createBorder(){
        //Define Layer
        self.layer.cornerRadius  = 8.0
        self.layer.masksToBounds = true
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多