//修改全局颜色 UINavigationBar.appearance().tintColor = UIColor.orangeColor()
UITabBar.appearance().tintColor = UIColor.orangeColor()
1.创建基本视图 baseView:UItableViewController 因为微博显示的是tablerview 所以基视图也是tableview
2.下面4个控制器(首页 发现 消息 我)都是继承baseView :uitableviewcontroller
3.在loadView中判断是否登录 (flag)如果为true 就显示微博信息(super.loadView) (因为控制器的关系是UItableViewController->basicView->homeviewcontroller basic如果调用父类的loadview方法 就是直接显示homeviewcontroller了) 如果false 就调用自己写的setupVisitor()方法来显示访客视图
4.visitorview:uiview(uiview就够了,)
*需要调用initwithframe构造方法 因为在手码布局时会调用这个方法 在这个方法里添加及布局控件
override init(frame: CGRect) { super.init(frame: frame) setupUI() } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }
*懒加载控件
/ MARK: - 懒加载控件 -> 负责创建控件 // 图标 private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon")) // 小房子 private lazy var homeIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house")) // 遮罩视图 - 不要使用 maskView private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon")) // 消息文字 private lazy var messageLabel: UILabel = { let label = UILabel() label.text = "关注一些人,回这里看看有什么惊喜关注一些人,回这里看看有什么惊喜" // 一般不要使用纯黑色 label.textColor = UIColor.darkGrayColor() label.font = UIFont.systemFontOfSize(14) label.numberOfLines = 0 label.textAlignment = NSTextAlignment.Center return label }() // 注册按钮 private lazy var registerButton: UIButton = { let button = UIButton() button.setTitle("注册", forState: UIControlState.Normal) button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal) button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal) return button }() // 登录按钮 private lazy var loginButton: UIButton = { let button = UIButton() button.setTitle("登录", forState: UIControlState.Normal) button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal) button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal) return button }()
*布局控件
private func setupUI() { // 1. 添加控件 addSubview(iconView) addSubview(maskIconView) addSubview(homeIconView) addSubview(messageLabel) addSubview(registerButton) addSubview(loginButton) // 2. 设置布局,将布局要添加到视图上 // "view1.attr1 = view2.attr2 * multiplier + constant" // 默认情况下,使用纯代码开发,是不支持自动布局的,如果要支持自动布局,需要将 // 控件的 translatesAutoresizingMaskIntoConstraints 设置为 false / NO // 1> 图标 iconView.translatesAutoresizingMaskIntoConstraints = false addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)) addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -60)) // 2> 小房子 - 代码设计自动布局的时候,最好有一个固定的参照物 homeIconView.translatesAutoresizingMaskIntoConstraints = false addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)) addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)) // 3> 设置文本 messageLabel.translatesAutoresizingMaskIntoConstraints = false addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)) addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16)) // 提示:如果要设置一个固定数值,参照的属性,需要设置为 NSLayoutAttribute.NotAnAttribute,参照对象是 nil addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224)) // 4> 注册按钮 registerButton.translatesAutoresizingMaskIntoConstraints = false addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0)) addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16)) addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100)) addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35)) // 5> 登录按钮 loginButton.translatesAutoresizingMaskIntoConstraints = false addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0)) addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16)) addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100)) addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
时间: 2024-11-08 20:10:59