如何快速控制容器视图的子视图?

时间:2019-07-11 10:12:02

标签: ios swift

我正在通过代码创建应用UI。

我通常使用容器视图(仅UIView)进行层次结构设计。

当我使用容器视图时,我觉得有更好的方法 比我使用的要多。

下面的代码是我编写的方式。

我将所有视图组件都设为全局变量。

并在initView()上设置它们

let conditionContainerView:UIView = {
        let view = UIView()
        return view
    }()

    let conditionSubContainerView:UIView = {
        let view = UIView()
        return view
    }()

    let firstButton:UIButton = {
        let btn = UIButton()
        btn.titleLabel?.font = UIFont.systemFont(ofSize:14, weight: .bold)
        return btn
    }()

    let secondButton:UIButton = {
        let btn = UIButton()
        btn.titleLabel?.font = UIFont.systemFont(ofSize:14, weight: .bold)
        return btn
    }()

    let thirdButton:UIButton = {
        let btn = UIButton()
        btn.titleLabel?.font = UIFont.systemFont(ofSize:14, weight: .bold)
        return btn
    }()

func initView() {
        view.backgroundColor = .white

        view.addSubview(conditionContainerView)
        conditionContainerView.addSubview(conditionSubContainerView)
        conditionSubContainerView.addSubview(firstButton)
        conditionSubContainerView.addSubview(secondButton)
        conditionSubContainerView.addSubview(thirdButton)

        view.addConstraintsWithFormat(format: "H:|[v0]|", views: conditionContainerView)
        view.addConstraintsWithFormat(format: "V:|[v0(35)]", views: conditionContainerView)

        conditionSubContainerView.addConstraintsWithFormat(format: "H:[v0(50)]-8-[v1(50)]-8-[v2(50)]", views: firstButton, secondButton, thirdButton)
        conditionSubContainerView.addConstraintsWithFormat(format: "V:|[v0]|", views: firstButton)
        conditionSubContainerView.addConstraintsWithFormat(format: "V:|[v0]|", views: secondButton)
        conditionSubContainerView.addConstraintsWithFormat(format: "V:|[v0]|", views: thirdButton)

        firstButton.setTitle("first", for: .normal)
        secondButton.setTitle("second", for: .normal)
        thirdButton.setTitle("third", for: .normal)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        initView()
    }

我使用这种方式的原因是我无法弄清楚访问容器视图子视图的方式。

这就是我要使用的。

let conditionContainerView:UIView = {
        let view = UIView()
        return view
    }()

    let conditionSubContainerView:UIView = {
        let view = UIView()

        let firstButton = UIButton()
        firstButton.titleLabel?.font = UIFont.systemFont(ofSize:14, weight: .bold)
        let secondButton = UIButton()
        secondButton.titleLabel?.font = UIFont.systemFont(ofSize:14, weight: .bold)
        let thirdButton = UIButton()
        thirdButton.titleLabel?.font = UIFont.systemFont(ofSize:14, weight: .bold)

        view.addSubview(firstButton)
        view.addSubview(secondButton)
        view.addSubview(thirdButton)

        view.addConstraintsWithFormat(format: "H:[v0(50)]-8-[v1(50)]-8-[v2(50)]", views: firstButton, secondButton, thirdButton)
        view.addConstraintsWithFormat(format: "V:|[v0]|", views: firstButton)
        view.addConstraintsWithFormat(format: "V:|[v0]|", views: secondButton)
        view.addConstraintsWithFormat(format: "V:|[v0]|", views: thirdButton)

        return view
    }()

func initView() {
        view.backgroundColor = .white

        view.addSubview(mapView)

        view.addSubview(conditionContainerView)
        conditionContainerView.addSubview(conditionSubContainerView)

        view.addConstraintsWithFormat(format: "H:|[v0]|", views: conditionContainerView)
        view.addConstraintsWithFormat(format: "V:|[v0(35)]", views: conditionContainerView)

        conditionSubContainerView.firstButton.setTitle("first", for: .normal)
        conditionSubContainerView.secondButton.setTitle("second", for: .normal)
        conditionSubContainerView.thirdButton.setTitle("third", for: .normal)

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        initView()
    }

但是我不能使用代码

conditionSubContainerView.firstButton.setTitle(“ first”,表示:.normal)

对某人来说,这也许是一个简单的问题。也许什么都没有。 但是我真的很想知道使代码更简洁更短的更好的方法。

谢谢您的建议。

2 个答案:

答案 0 :(得分:1)

您需要从UIView继承

final class SomeView: UIView {
    lazy var button: UIButton = {
        return UIButton()
    }()
}

然后进入视图控制器:

class ViewController: UIViewController {
  let containerView = SomeView()

  override func loadView() {
      view = containerView
  }
}

并使用containerView代替self.view

func videDidLoad() {
   super.viewDidLoad()

   containerView.button.setTitle("Test", for: .normal)
}

答案 1 :(得分:1)

解决此问题完全取决于您的用例:-

如果您打算从视图中大量访问这些按钮,并且要共享很多按钮/元素,建议您创建一个自定义UIView并使这些元素可公开访问

否则,如果这是一次性过程,则只需在实例化按钮后立即为其分配标题

最后,仅是为了回答您的问题,这是一种效率不高的解决方法(尤其是在处理大量子视图时)。您的情况下的三个按钮没什么大不了的。将标签分配给这些按钮,并在最终感觉需要时对其进行过滤:-

let conditionSubContainerView:UIView = {
    let view = UIView()

    let firstButton = UIButton()
    firstButton.tag = 1 // <- assign tags this way
    firstButton.titleLabel?.font = UIFont.systemFont(ofSize:14, weight: .bold)
    let secondButton = UIButton()
    secondButton.tag = 2
    secondButton.titleLabel?.font = UIFont.systemFont(ofSize:14, weight: .bold)
    let thirdButton = UIButton()
    thirdButton.tag = 3
    thirdButton.titleLabel?.font = UIFont.systemFont(ofSize:14, weight: .bold)

    view.addSubview(firstButton)
    view.addSubview(secondButton)
    view.addSubview(thirdButton)

    return view
}()

并使用您为其分配的标签过滤它们:-

if let yourButton = conditionSubContainerView.subviews.filter({$0.tag == 1}).first as? UIButton{
    print("This works and you'll get your firstButton but have this as your last option")
}