我正在尝试创建一个 UIView A
,它有一个 UIView B
,它以 UIView A
为中心,并包含两个 UILabel A 和 B
,但它没有将 UILabels
居中。这是我的代码
private func generateView(header: String, below: String) {
let size: CGSize = self.view.frame.size
let mainView = UIView(frame: CGRectMake(0, 0, size.width, size.height))
mainView.backgroundColor = UIColor.clearColor()
let contentHolderView = UIView(frame: CGRectMake(0, 0, 350, 172))
contentHolderView.backgroundColor = UIColor.clearColor()
let headerLabel = UILabel(frame: CGRectMake(38, 24, 298, 34))
let belowLabel = UILabel(frame: CGRectMake(14, 66, 344, 83))
belowLabel.numberOfLines = 0
headerLabel.text = header
belowLabel.text = below
headerLabel.textColor = UIColor.blackColor()
belowLabel.textColor = UIColor.blackColor()
headerLabel.font = UIFont(name: "BrandonGrotesque-Bold", size: 30)
belowLabel.font = UIFont(name: "BrandonGrotesque-Light", size: 20)
headerLabel.textAlignment = .Center
belowLabel.textAlignment = .Center
contentHolderView.addSubview(headerLabel)
contentHolderView.addSubview(belowLabel)
contentHolderView.center = mainView.center
mainView.addSubview(contentHolderView)
mainView.center = self.view.center
self.view.addSubview(mainView)
}
这就是我想要实现的目标,这就是我得到的以及我想要实现的目标
最佳答案
与其尝试直接对帧值进行编码,我建议您采用自动布局。这是一个 UIView 子类的基础,它可以做你想做的事(我没有为字体烦恼,但你可以很容易地添加它)-
import UIKit
@IBDesignable
class MyView: UIView {
var headerLabel:UILabel!
var bodyLabel:UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
self.addLabels()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.addLabels()
}
func addLabels() {
self.translatesAutoresizingMaskIntoConstraints=false
self.headerLabel=UILabel()
self.headerLabel.translatesAutoresizingMaskIntoConstraints=false
self.headerLabel.textAlignment=NSTextAlignment.Center
self.addSubview(self.headerLabel)
self.bodyLabel=UILabel()
self.bodyLabel.translatesAutoresizingMaskIntoConstraints=false
self.bodyLabel.numberOfLines=0
self.bodyLabel.textAlignment=NSTextAlignment.Center
self.addSubview(self.bodyLabel)
}
override func updateConstraints() {
self.addConstraint(NSLayoutConstraint(item: self.headerLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 24))
self.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.headerLabel, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.bodyLabel, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.bodyLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 24))
self.addConstraint(NSLayoutConstraint(item: self.bodyLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.headerLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 12))
self.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.LeadingMargin, relatedBy: NSLayoutRelation.Equal, toItem: self.bodyLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.TrailingMargin, relatedBy: NSLayoutRelation.Equal, toItem: self.bodyLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0))
super.updateConstraints()
}
override func prepareForInterfaceBuilder() {
self.headerLabel.text="Header"
self.bodyLabel.text="Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum "
}
}
关于ios - 创建带标签的居中 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33880150/