Button(实训)

来源:互联网 发布:做什么网络兼职赚钱 编辑:程序博客网 时间:2024/06/03 13:45

前面讲了textfield,这里说说button,有异曲同工之处。登录注册界面里面用到了一些button,而且各式各样。还是先贴代码!

import UIKitclass SignViewController: UIViewController, UITextFieldDelegate {    var loginBtn : UIButton!    var forgetBtn : UIButton!    var registBtn : UIButton!    var loginLable : UILabel!    var weixinBtn : UIButton!    var weiboBtn : UIButton!    var qqBtn : UIButton!    let screenwidth = UIScreen.main.applicationFrame.size.width    let screenheight = UIScreen.main.applicationFrame.size.height        let notification = NotificationCenter.default        // MARK: - Button Action        func loginButtonTapped(_ sender: UIButton) {        guard isValidPhone(phoneText.text) else {            //self.noticeOnlyText("请输入合法手机号")            return        }                textFieldResignFirstResponder()                UserManager.shared.login(tel: phoneText.text!,password: passwordText.text!)    }        func showSignupButtonTapped(_ sender: UIButton) {        let registViewController = RegisterViewController()        self.navigationController?.pushViewController(registViewController, animated: true)    }    func forget(){        let secondViewController = forgetViewController()        self.navigationController!.pushViewController(secondViewController, animated: true)    }    func weibo(){            }    func weixin(){            }    func qq(){            }        override func viewDidLoad() {        super.viewDidLoad()        self.navigationItem.title = "登录"        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "sign_bg")!)        self.view.contentMode = .scaleAspectFill                loginBtn = UIButton(frame: CGRect(x: 50, y: 380, width: screenwidth-100, height: 40))        loginBtn.setTitle("登录", for: .normal)        loginBtn.backgroundColor = UIColor(red: 49/255.0, green: 181/255.0, blue: 142/255.0, alpha: 1.0)        loginBtn.layer.cornerRadius=10;        loginBtn.addTarget(self, action: #selector(loginButtonTapped), for: .touchUpInside)                forgetBtn = UIButton(frame: CGRect(x: screenwidth/2-70, y: 440, width: 70, height: 40))        forgetBtn.setTitle("忘记密码", for: .normal)        forgetBtn.titleLabel?.textColor=kRGBColorFromHex(rgbValue: 0x656565)        forgetBtn.titleLabel?.font=UIFont.systemFont(ofSize: 14)        forgetBtn.addTarget(self, action: #selector(forget), for: .touchUpInside)                registBtn = UIButton(frame: CGRect(x: screenwidth/2, y: 440, width: 70, height: 40))        registBtn.setTitle("立即注册", for: .normal)        registBtn.titleLabel?.textColor=kRGBColorFromHex(rgbValue: 0x656565)        registBtn.titleLabel?.font=UIFont.systemFont(ofSize: 14)        registBtn.addTarget(self, action: #selector(showSignupButtonTapped(_:)), for: .touchUpInside)                weiboBtn = UIButton(frame: CGRect(x: screenwidth/2-100, y: 575, width: 50, height: 50))        weiboBtn.setImage(UIImage(named: "weibo_icon_png.png"), for: .normal)        weiboBtn.contentMode = .scaleAspectFill        weiboBtn.layer.masksToBounds = true        //设置圆角半径(宽度的一半),显示成圆形。        weiboBtn.layer.cornerRadius = weiboBtn.frame.width/2        weiboBtn.addTarget(self, action: #selector(weibo), for: .touchUpInside)        weixinBtn = UIButton(frame: CGRect(x: screenwidth/2-25, y: 575, width: 50, height: 50))        weixinBtn.setImage(UIImage(named: "wechat_icon_png.png"), for: .normal)        weixinBtn.contentMode = .scaleAspectFill        weixinBtn.layer.masksToBounds = true        //设置圆角半径(宽度的一半),显示成圆形。        weixinBtn.layer.cornerRadius = weixinBtn.frame.width/2        weixinBtn.addTarget(self, action: #selector(weixin), for: .touchUpInside)        qqBtn = UIButton(frame: CGRect(x: screenwidth/2+50, y: 575, width: 50, height: 50))        qqBtn.setImage(UIImage(named: "qq_icon_png.png"), for: .normal)        qqBtn.contentMode = .scaleAspectFill        qqBtn.layer.masksToBounds = true        //设置圆角半径(宽度的一半),显示成圆形。        qqBtn.layer.cornerRadius = qqBtn.frame.width/2        qqBtn.addTarget(self, action: #selector(qq), for: .touchUpInside)                loginLable = UILabel(frame: CGRect(x: 50, y: 530, width: screenwidth-100, height: 20))        loginLable.textAlignment = .center        loginLable.text = "—————  第三方登录  —————"        loginLable.textColor = kRGBColorFromHex(rgbValue: 0xffffff)                self.view.addSubview(loginBtn)        self.view.addSubview(forgetBtn)        self.view.addSubview(registBtn)        self.view.addSubview(loginLable)        self.view.addSubview(weiboBtn)        self.view.addSubview(weixinBtn)        self.view.addSubview(qqBtn)    }            func kRGBColorFromHex(rgbValue: Int) -> (UIColor) {        return UIColor(red: ((CGFloat)((rgbValue & 0xFF0000) >> 16)) / 255.0,green: ((CGFloat)((rgbValue & 0xFF00) >> 8)) / 255.0,blue: ((CGFloat)(rgbValue & 0xFF)) / 255.0,alpha: 1.0)    }}

这里有好几种类型的button,首先是我们常用的。基本上需要的一些属性设置都跟前面textfield差不多。不多说,就是说一下给button添加点击事件,写一个点击的时候的方法,绑定一下就行,很方便。

然后就是平时用的第三方登录的按钮了,跟平常的不太一样。首先是圆形的,而且直接给他设置成我们的图片。要设置成圆形用圆角即可,frame.width/2。然后用setImage设置图片就好了。但是这里出了点小问题,图片跟按钮大小不太匹配,不是想要的样子,后来发现是填充方式的问题。按下面这样设置,图片就会自动匹配,填充成我们button的大小了。

qqBtn.contentMode = .scaleAspectFill

原创粉丝点击