• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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!)
}

}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swifttableview自带的刷新控件发布时间:2022-07-13
下一篇:
[Swift]LeetCode885.螺旋矩阵III|SpiralMatrixIII发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap