Don’t Put View Code Into Your View Controller别把View创建的代码放在VC中

Don’t Put View Code Into Your View Controller别把View创建的代码放在VC中

Don’t Put View Code Into Your View Controller别把View创建的代码放在VC中

Sometimes I see code that sets up subviews in viewDidLoad() of a view controller. Be a good citizen and don’t do that. If you have something to add to a view, do it in a UIView subclass.

Here is how I implement view and controller code when I’m not using Interface Builder.

The View

Let’s build a login view:

This code defines two text fields and a button and puts it onto the view with the help of UIStackView (line 26 and 31). If you haven’t played with stack views yet, start now. They are awesome!

Note that the layout constraints don’t define the vertical position of the UIStackView on the view. We will add vertical constraints in the view controller.

 
   
    let textFieldStackView = UIStackView(arrangedSubviews: [usernameTextField, passwordTextField])
    textFieldStackView.axis = .Vertical
    textFieldStackView.spacing = 10
    textFieldStackView.distribution = .FillEqually
  • axis 整体分布方式(水平,垂直)
  • spacing 元素间距
  • distribution 元素之间的分布方式

The View Controller

Here comes the view controller:

If you build your user interface in code you should override loadView(). Within loadView() you need to set the view property of the view controller. (如果使用代码创建UI,需要重载load view()方法,在loadView中需要设置view属性)In this example we additionally add a target to the login button.

Note the definition of the calculated property loginView. This is done to allow the access of the loginView’s stackView in viewWillLayoutSubviews().

The view controller can be presented within a navigation controller (and in fact is in the demo project). To position the stack view in respect to the bottom edge of the navigation bar we use the topLayoutGuide of the view controller. This takes into account the navigation bar. This means the code viewWillLayoutSubviews() puts the stack view 8 points below the navigation bar.

The so defined login screen looks like this:

You can find the sample code on github. In the sample project you can also find three other possibilities to define the layout and in addition the version using Interface Builder.

If you enjoyed this post, then make sure you subscribe to my feed.

原文链接

http://swiftandpainless.com/dont-put-view-code-into-your-view-controller/

代码

import UIKit

class LoginStackView: UIView {

let usernameTextField: UITextField
let passwordTextField: UITextField
let button: UIButton
let stackView: UIStackView

override init(frame: CGRect) {
    usernameTextField = UITextField(frame: CGRect.zero)
    usernameTextField.placeholder = "username"
    usernameTextField.borderStyle = .Line

    passwordTextField = UITextField(frame: CGRect.zero)
    passwordTextField.secureTextEntry = true
    passwordTextField.placeholder = "password"
    passwordTextField.borderStyle = .Line

    button = UIButton(type: .System)
    button.setTitle("Login", forState: .Normal)
    button.backgroundColor = UIColor.grayColor()
    button.tintColor = UIColor.whiteColor()
    button.layer.cornerRadius = 5

    //UIStackView的用法

    let textFieldStackView = UIStackView(arrangedSubviews: [usernameTextField, passwordTextField])
    textFieldStackView.axis = .Vertical
    textFieldStackView.spacing = 10
    textFieldStackView.distribution = .FillEqually

    stackView = UIStackView(arrangedSubviews: [textFieldStackView,button])
    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.axis = .Vertical
    stackView.spacing = 20

    super.init(frame: frame)

    backgroundColor = UIColor.whiteColor()
    addSubview(stackView)

    //MARK: - Layout
    let views = ["stackView": stackView]
    var layoutContraints = [NSLayoutConstraint]()

    layoutContraints += NSLayoutConstraint.constraintsWithVisualFormat("|-[stackView]-|", options: [], metrics: nil, views: views)
    layoutContraints.append(usernameTextField.heightAnchor.constraintEqualToConstant(30))
    layoutContraints.append(button.heightAnchor.constraintEqualToConstant(40))

    NSLayoutConstraint.activateConstraints(layoutContraints)

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

import UIKit

class ViewController: UIViewController {

var loginView: LoginStackView{
    return view as! LoginStackView
}

override func loadView() {
    let contentView = LoginStackView(frame: CGRect.zero)

    contentView.button.addTarget(self, action: "login", forControlEvents: .TouchUpInside)

    view = contentView
}

override func viewWillLayoutSubviews() {
    loginView.stackView.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor, constant: 50)
}

func login(){
   print("username: " + loginView.usernameTextField.text! + " password: " + loginView.passwordTextField.text!)
}

}

原文地址:https://www.cnblogs.com/xilifeng/p/4777485.html