swift基本的页面搭建

一:创建自己的ViewController视图

1.是否改变跟视图

let VC = TestViewContorller()

        letnavVC =UINavigationController(rootViewController: VC)

        self.window?.rootViewController= navVC

2.像这样的先做一个,方便我们调节位置

let SCR_W = UIScreen.main.bounds.size.width

let SCR_H = UIScreen.main.bounds.size.height

3.根据自己的喜好来创建,就做一个简单的登录界面吧

(1)在ViewDidLoad里

self.view.backgroundColor = UIColor.gray

        //左侧按钮

        letLeftBtn =UIButton(frame:CGRect(x:0, y:0, width:50, height:50))

        LeftBtn.setImage(UIImage(named:"返回箭头"), for: .normal)

        letleftItem =UIBarButtonItem(customView: LeftBtn)

        self.navigationItem.leftBarButtonItem = leftItem

        //导航条标题

        self.navigationItem.title = "导航条文字"

        layout()

(2)实现上面的方法

funclayout() ->Void{

        //头像框

        letImgV =UIImageView(image:UIImage(named:"头像照片"))

        ImgV.frame=CGRect(x: (SCR_W-120)/2, y:150, width:120, height:120)

        ImgV.layer.cornerRadius = 60

        ImgV.layer.masksToBounds = true

        self.view.addSubview(ImgV)

        //登录框背景

        letvLogin =UIView(frame:CGRect(x:0, y:300, width:SCR_W, height:180))

        vLogin.layer.borderWidth=0.5

        vLogin.layer.borderColor = UIColor.lightGray.cgColor

        vLogin.backgroundColor = UIColor.white

        self.view.addSubview(vLogin)

        //用户输入框

        lettxtUser =UITextField(frame:CGRect(x:0, y:0, width:SCR_W, height:60))

        txtUser.placeholder=" 请输入账号"

        txtUser.layer.borderWidth=0.5

        txtUser.leftView=UIView(frame:CGRect(x:0, y:0, width:80, height:60))

        txtUser.leftViewMode = UITextFieldViewMode.always

        //用户名输入框左侧图标

        letaccountBtn =UIButton()

        accountBtn.frame=CGRect(x:0, y:0, width:80, height:60)

        accountBtn.setImage(UIImage(named:"账号"), for: .normal)

        accountBtn.alpha=0.7

        accountBtn.setTitle("  账号  | ", for: .normal)

        accountBtn.setTitleColor(UIColor.black, for: .normal)

        txtUser.leftView?.addSubview(accountBtn)

        vLogin.addSubview(txtUser)

        //用户密码框

        lettxtPassWord =UITextField(frame:CGRect(x:0, y:60, width:SCR_W, height:60))

        txtPassWord.placeholder="请输入密码"

        txtPassWord.layer.borderWidth=0.5

        txtPassWord.leftView=UIView(frame:CGRect(x:0, y:60, width:80, height:60))

        txtPassWord.leftViewMode = UITextFieldViewMode.always

        //密码输入框左侧图标

        letpasswordBtn =UIButton()

        passwordBtn.frame=CGRect(x:0, y:0, width:80, height:60)

        passwordBtn.setImage(UIImage(named:"锁"), for: .normal)

        passwordBtn.alpha=0.7

        passwordBtn.setTitle("  密码  | ", for: .normal)

        passwordBtn.setTitleColor(UIColor.black, for: .normal)

        txtPassWord.leftView?.addSubview(passwordBtn)

        vLogin.addSubview(txtPassWord)

        //确认用户密码框

        lettxtConfirmPW =UITextField(frame:CGRect(x:0, y:120, width:SCR_W, height:60))

        txtConfirmPW.placeholder="请再次输入密码"

        txtConfirmPW.layer.borderWidth=0.5

        txtConfirmPW.leftView=UIView(frame:CGRect(x:0, y:120, width:80, height:60))

        txtConfirmPW.leftViewMode = UITextFieldViewMode.always

        //确认密码图标

        letConfirmPWBtn =UIButton()

        ConfirmPWBtn.frame=CGRect(x:0, y:0, width:80, height:60)

        ConfirmPWBtn.setImage(UIImage(named:"锁"), for: .normal)

        ConfirmPWBtn.alpha=0.7

        ConfirmPWBtn.setTitle("  密码  | ", for: .normal)

        ConfirmPWBtn.setTitleColor(UIColor.black, for: .normal)

        txtConfirmPW.leftView?.addSubview(ConfirmPWBtn)

        vLogin.addSubview(txtConfirmPW)

        //注册按钮

        letRegistered =UIButton(frame:CGRect(x: (SCR_W-350)/2, y:500, width:350, height:55))

        Registered.setTitle("注册", for:.normal)

        Registered.backgroundColor=UIColor(red: (0/255.0), green: (193/255.0), blue: (255/255.0), alpha:1.0)

        Registered.layer.cornerRadius=5

        Registered.layer.masksToBounds=true

        Registered.addTarget(self, action:#selector(click), for: .touchUpInside)

        self.view.addSubview(Registered)


        //判断是否为有的账号

        letuserLab =UILabel(frame:CGRect(x:270, y:570, width:80, height:32))

        userLab.text="?"

        userLab.font=UIFont.systemFont(ofSize:15)

        userLab.textColor=UIColor.lightGray

        self.view.addSubview(userLab)

        letuserBtn =UIButton(frame:CGRect(x:350, y:575, width:50, height:20))

        userBtn.setImage(UIImage(named:"登录"), for: .normal)

        userBtn.addTarget(self, action:#selector(login), for: .touchUpInside)

        self.view.addSubview(userBtn)

    }

(3)实现我们上面的点击方法

@objcfuncclick() ->Void{

        let NetWork = NetworkViewController()

        self.navigationController?.pushViewController(NetWork, animated:true)

        letitem =UIBarButtonItem(title:"", style: .plain, target:self, action:nil)

        self.navigationItem.backBarButtonItem = item

    }

(4)做好之后我们一定要判断此账号是否登录

@objcfunclogin() ->Void{

        print("登录异地登录,请注意安全")

    }

你可能感兴趣的:(swift基本的页面搭建)