swift微博第5天(未登录界面的布局)

  • 1.自定义一个VisitorView 的UIView来展示未登录的情况

     // 定义一个变量保存当前用户是否登录
     var userLogin = false
    
     override func loadView() {
      
      userLogin ? super.loadView() : setViewUp()
      
      }
     // MARK: 内部控制方法
     /*
      * 创建未登录界面
      */
     private func  setViewUp(){
      
      print("哈哈")
      
      let customView = VisitorView()
      customView.backgroundColor = UIColor.JKGlobalColor()
      view = customView
      
     }
    
  • 重写View 的一些设置(一定要写上两个方法)

      下面的两个方法
    
        override init(frame: CGRect) {
     
       super.init(frame: frame)
    
      }
    
      // swift推荐我们自定义一个控件,要么用纯代码,要么用xib或者stroyboard
      required init?(coder aDecoder: NSCoder) {
     
        // 如果通过xib或者stroyboard创建该类,那么就会崩溃
        fatalError("init(coder:) has not been implemented")
      }
    
  • 具体的重写view里面的

        import UIKit
    

class VisitorView: UIView {

override init(frame: CGRect) {
    
    super.init(frame: frame)
    
    // 1.添加子控件
    addSubview(iconView)
    
    addSubview(homeIcob)
    addSubview(maskBGView)
    addSubview(messageLabel)
    addSubview(loginButton)
    addSubview(zhuceBtn)
    // 2.0布局子控件
    // 2.1.设置背景圈圈
    iconView.centerX = JKscreenW/2.0
    iconView.centerY = JKscreenH/2.0
    // 2.2.设置中间的小房子
    homeIcob.centerX = JKscreenW/2.0
    homeIcob.centerY = JKscreenH/2.0
    // 2.3.设置文本
    messageLabel.y = iconView.frame.maxY + 10
    messageLabel.centerX = JKscreenW/2.0
    // 2.4.设置按钮
    zhuceBtn.x = messageLabel.x
    zhuceBtn.y = messageLabel.frame.maxY + 10
    
    loginButton.x = messageLabel.frame.maxX - 100
    loginButton.y = messageLabel.frame.maxY + 10
    
    // 2.5.设置蒙版
    maskBGView.centerX = JKscreenW/2.0
    maskBGView.centerY = JKscreenH/2.0
    
}
// swift推荐我们自定义一个控件,要么用纯代码,要么用xib或者stroyboard
required init?(coder aDecoder: NSCoder) {
    
    // 如果通过xib或者stroyboard创建该类,那么就会崩溃
    fatalError("init(coder:) has not been implemented")
}

// MARK: 懒加载一些控件
// 转盘
private lazy var iconView: UIImageView = {

    let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    return iv

}()

// 图标
private lazy var homeIcob: UIImageView = {

    let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    return iv

}()
// 文本
private lazy var messageLabel: UILabel = {

    let label = UILabel()
    label.height = NSString.JKtextStringHeight(string:"三月,醉一场青春的流年。慢步在三月的春光里,走走停停,看花开嫣然,看春雨绵绵,感受春风拂面,春天,就是青春的流年。", size: CGSize(width:224,height:1000), fontSize: 14)
    label.width = 224
    label.font = UIFont.systemFont(ofSize: 14)
    label.numberOfLines = 0
    label.textAlignment = NSTextAlignment.center
    label.textColor = UIColor.JKTextGayColor()
    label.text = "三月,醉一场青春的流年。慢步在三月的春光里,走走停停,看花开嫣然,看春雨绵绵,感受春风拂面,春天,就是青春的流年。"
    
    return label

}()

// 登录按钮
private lazy var loginButton: UIButton = {

    let login = UIButton()
    login.width = 100
    login.height = 30
    login.setTitleColor(UIColor.JKTextGayColor(), for: UIControlState.normal)
    login.setTitle("登录", for: UIControlState.normal)
    login.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControlState.normal)
    return login
    
}()

// 注册按钮
private lazy var zhuceBtn: UIButton = {

    let zhuce = UIButton()
    zhuce.width = 100
    zhuce.height = 30
    zhuce.setTitleColor(UIColor.orange, for: UIControlState.normal)
    zhuce.setTitle("注册", for: UIControlState.normal)
    zhuce.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControlState.normal)
    return zhuce
    
}()

// 蒙版的添加
private lazy var maskBGView: UIImageView = {
    // UIImageView 的初始化
    let maskbgView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
    // UIImageView 的frame
    //  maskbgView.frame = CGRect(x:10,y:10,width:100,height:100)
    // UIImageView 的图片填充方式
    maskbgView.contentMode = UIViewContentMode.scaleAspectFill
    return maskbgView

  }()

}

你可能感兴趣的:(swift微博第5天(未登录界面的布局))