2017-08-30 20 views
0

我想将所有样式信息添加到类中,然后子类UIButton以避免重复代码。Swift:将CGSize应用于按钮类不改变

此刻,我的课是这样的:

class CustomButton: UIButton { 


    required init() { 

    super.init(frame: .zero) 

    // set other operations after super.init, if required 
    backgroundColor = .red 
    layer.cornerRadius = 5 
    layer.borderWidth = 1 
    layer.borderColor = UIColor.black.cgColor 
    frame.size = CGSize(width: 700, height: 100) 

    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

} 

在viewDidLoad中我加入:

let b1 = CustomButton() 

    view.addSubview(b1) 

    // auto layout 
    b1.translatesAutoresizingMaskIntoConstraints = false 
    b1.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
    b1.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true 

正如你可以在课堂上看到我已经设置了frame.size

frame.size = CGSize(width: 700, height: 100) 

但是,当我运行它时,它看起来像这样:

enter image description here

宽度显然不是。任何建议,我哪里出错了?

回答

1

问题是你在混合框架和自动布局。具体而言,一旦关闭自动调整大小,框架就会消失。为什么不做100%的自动布局?事实上,为什么不让centerX/centerY成为init()的一部分?

class CustomButton: UIButton { 

    required init(width:CGFloat, height:CGFloat, centerButton:Bool) { 
     super.init(frame: .zero) 

     // set other operations after super.init, if required 
     backgroundColor = .red 
     layer.cornerRadius = 5 
     layer.borderWidth = 1 
     layer.borderColor = UIColor.black.cgColor 
     self.translatesAutoresizingMaskIntoConstraints = false 
     self.widthAnchor.constraint(equalToConstant: width).isActive = true 
     self.heightAnchor.constraint(equalToConstant: height).isActive = true 
     if centerButton { 
      self.centerXAnchor.constraint(equalTo: superview?.centerXAnchor).isActive = true 
      self.centerYAnchor.constraint(equalTo: superview?.centerYAnchor).isActive = true 
     } 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

} 

,改变你的呼叫viewDidLoad()于:(我加入了宽/高规格,以init()使代码更灵活)

let b1 = CustomButton(width:700, height:100, centerButton:true) 

编辑:问候我的最后(加括号)声明。如果你要做的就是更换:

frame.size = CGSize(width: 700, height: 100) 

有:

self.widthAnchor.constraint(equalToConstant: 700).isActive = true 
self.heightAnchor.constraint(equalToConstant: 100).isActive = true 

一切都会解决的。但是,在你的问题,你也说(重点煤矿):

我想添加的所有样式信息到一个类,然后子类 的UIButton到的代码避免重复。

虽然代码是未经测试,我在努力使代码更适应在飞行中创建一个按钮添加参数到init。根据您的需求,您可以将其扩展到从backgroundColorcornerRadius的所有内容。

我来自OOP背景(实际上是70年代的自上而下),所以子类对我来说太直观了。我所介绍的只是 - 子类,以避免重复的代码。斯威夫特提出了新的方法 - 特别是extensionconvenience init。我认为这些都可以为你工作。我不确定扩展与子类化的具体优点/缺点 - 我的感觉是代码的重复对于两者来说大致相同(技术上) - 但我会总是欣赏“现代”语言带来了什么开发人员的工具包!

+0

在你解释中央按钮布尔和它为什么在那里?我最终会在视图控制器上有不同位置的6个按钮。这个uibutton只针对这个问题。其他人将在屏幕上的radom地方 – JamesG

+0

我已经接受了答案,如果我拿出所有中心的东西,它的工作完美:) – JamesG

+0

把这件事(并理解我张贴未经测试的代码,你可能有更多的代码张贴) ......(1)底线问题是混合帧和约束,同时关闭自动调整大小。我会发布一个编辑到我的答案来解释更多。 (2)您提到 - 我同意 - 避免重复代码。我的尝试不仅是修复特定的问题,而且还要尽可能多地添加到'init'方法中。因此,宽度/高度*和*是否需要每个按钮居中,或者像您指示的那样,而不是。事实上...... – dfd

0

要决定任何视图的位置或大小,您可以使用 基于框架的布局或基于约束的Autolayout不能同时使用。

谢谢