我在向堆栈视图添加右上角和左上角按钮时出现问题。通过界面构建器添加的按钮,然后由扩展名舍入,将呈现,但是如果通过代码添加按钮则不会。可能还有一些IB适用的附加属性。
以下是代码的摘录,说明了问题。
import UIKit
class TestViewController: UIViewController {
let colorDictionary = ["Red":UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0),"Green":UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0),"Blue":UIColor(red: 0.0, green: 0.0, blue: 1.0, alpha: 1.0),]
func colorButton(withColor color:UIColor, title:String) -> UIButton{
let newButton = UIButton(type: .system)
newButton.backgroundColor = color
newButton.setTitle(title, for: .normal)
newButton.setTitleColor(UIColor.white, for: .normal)
//Buttons fail to render if roundedButton() is applied
//**************************
//newButton.roundedButton()
//**************************
return newButton
}
override func viewDidLoad() {
super.viewDidLoad()
displayButtonsInStackView()
}
func displayButtonsInStackView(){
//generate an array
var buttonArray = [UIButton]()
for (myKey,myValue) in colorDictionary{
buttonArray += [colorButton(withColor: myValue, title: myKey)]
}
let stackView = UIStackView(arrangedSubviews: buttonArray)
stackView.axis = .horizontal
stackView.distribution = .fillEqually
stackView.alignment = .fill
stackView.spacing = 5
stackView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(stackView)
//autolayout the stack view - pin 30 up 20 left 20 right 30 down
let viewsDictionary = ["stackView":stackView]
let stackView_H = NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[stackView]-20-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
let stackView_V = NSLayoutConstraint.constraints(withVisualFormat: "V:|-30-[stackView]-30-|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: viewsDictionary)
view.addConstraints(stackView_H)
view.addConstraints(stackView_V)
}
}
extension UIButton {
func roundedButton(){
let maskPath = UIBezierPath(roundedRect: self.bounds,
byRoundingCorners: [.bottomRight , .topRight],
cornerRadii:CGSize(width:8.0, height:8.0))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.bounds
maskLayer.path = maskPath.cgPath
self.layer.mask = maskLayer
}
}
答案 0 :(得分:1)
问题是你的按钮没有尺寸。改变这个:
// ...
newButton.setTitleColor(UIColor.white, for: .normal)
newButton.roundedButton()
// ...
到此:
// ...
newButton.setTitleColor(UIColor.white, for: .normal)
newButton.sizeToFit()
newButton.roundedButton()
// ...