Swift:如何以编程方式创建可重用的视图?

时间:2018-03-14 17:04:52

标签: ios swift

假设我正在描述自己的UIView,我们称之为HeaderView。我希望HeaderView具有完全相同的属性,但只在标签文本中有所不同。以下是我目前的方式:

private let headerView: UIView = {
    let screenSize = UIScreen.main.bounds
    let screenWidth = screenSize.width
    let screenHeight = screenSize.height

    let view = UIView()
    view.backgroundColor = .white
    view.heightAnchor.constraint(equalToConstant: 65).isActive = true

    let label = UILabel()
    label.font = UIFont.systemFont(ofSize: 30)
    label.textAlignment = .left
    label.textColor = .black
    label.text = "Search"

    view.addSubview(label)
    label.translatesAutoresizingMaskIntoConstraints = false
    label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8).isActive = true
    label.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 5).isActive = true

    return view
}()

我如何使用它:

 view.addSubview(headerView)
 headerView.translatesAutoresizingMaskIntoConstraints = false
 headerView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
 headerView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
 headerView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true

如果我想要3个带有不同文本的标题视图怎么办?我如何将其变成可重复使用的程序化视图?

2 个答案:

答案 0 :(得分:1)

您可以创建UIView的子类并在任何地方重复使用

class HeaderView: UIView {

  let innerview = UIView()
  let innerlabel = UILabel() 

override init(frame: CGRect) {
    super.init(frame: frame)
    sharedLayout()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    sharedLayout()
}

private func sharedLayout() {

    self.addSubview(innerview)
    self.innerView.backgroundColor = UIColor.red
    innerview.translatesAutoresizingMaskIntoConstraints = false
    innerview.trailingAnchor.constraint(equalTo:self.trailingAnchor).isActive = true
    innerview.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
    innerview.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    innerview.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true

    // configure other items here
}

}

答案 1 :(得分:0)

您可以使用函数代替变量:

func buildHeaderView(withText text: String) -> UIView {
    let screenSize = UIScreen.main.bounds
    let screenWidth = screenSize.width
    let screenHeight = screenSize.height

    let view = UIView()
    view.backgroundColor = .white
    view.heightAnchor.constraint(equalToConstant: 65).isActive = true

    let label = UILabel()
    label.font = UIFont.systemFont(ofSize: 30)
    label.textAlignment = .left
    label.textColor = .black
    label.text = text

    view.addSubview(label)
    label.translatesAutoresizingMaskIntoConstraints = false
    label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8).isActive = true
    label.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 5).isActive = true

    return view
}

现在您可以使用此功能,如:

let searchHeaderView = buildHeaderView(withText: "Search")
view.addSubview(searchHeaderView)

let otherView = buildHeaderView(withText: "Other")