ios - 创建带标签的居中 View

标签 ios swift uiview

我正在尝试创建一个 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)
}

这就是我想要实现的目标,这就是我得到的以及我想要实现的目标

My Achievement

最佳答案

与其尝试直接对帧值进行编码,我建议您采用自动布局。这是一个 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/

相关文章:

ios - OpenCV findContours 崩溃

当 UIView 绘制导致应用程序崩溃时,ios opengles 显示渲染缓冲区以供查看

ios - 如何在多个uiview之间滑动

iphone - ' channel 未启动 : onDOMContentLoaded' error throwing in Phonegap iPhone

ios - Segue 导致 UITabBarController 在 Swift 中向上移动状态栏?

ios - 无法安装 iOS MDM 配置文件

ios - Swift Realm - 查询空集但数据位于 RealmBrowser 中

ios - 为什么必须将 View 属性分配给变量?

ios - 如何使用 alamofire 管理器在 ios app swift4 中添加 ssl 证书固定?

iphone - ImageView 的 UIView 动画框架