Swift - UIView,UILabel,UIButton,UIImageView

来源:互联网 发布:cisco查看端口流量 编辑:程序博客网 时间:2024/05/16 08:46

学习Swift没少查资料,但是发现网上都是对一些基础语法的介绍,还有些看不明白的,博主觉得么,没必要看的那么详细,等使用中自然就会懂了。但是使用的时候,我们最常用的UIView,UILabel,UIButton,UIImageView却没有说怎么用,今天博主就来介绍这些我们常用的控件怎么用,有了这些控件的使用方法,普通界面我们已经能够随手搭出来了:
代码统一放,博主简单写了个工程

import UIKitclass ViewController: UIViewController {    var myView = UIView()    var myLabel = UILabel()    var myButton = UIButton()    var myImageView = UIImageView()    override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.        self.toCreatAUIView()        self.toCreatAUILabel()        self.toCreatAUIButton()        self.creatAUIImageView()    }    /**     toCreatAUIView     */    func toCreatAUIView () {        myView.frame = CGRectMake(0, 0, 320, 568)        myView.backgroundColor = UIColor.blueColor()        self.view.addSubview(myView)    }    /**     toCreatAUILabel     */    func toCreatAUILabel()  {        myLabel.frame=CGRectMake(10, 20, 300, 60);        myLabel.text = "This is a UILabel!"        myLabel.backgroundColor = UIColor.redColor()        myLabel.textColor = UIColor.whiteColor()        myLabel.textAlignment = NSTextAlignment.Center        myLabel.layer.borderWidth = 1        myView.addSubview(myLabel)    }    /**     toCreatAUIButton     */    func toCreatAUIButton()  {        myButton.frame = CGRectMake(10, 100, 300, 60);        myButton.setTitle("This is a UIButton", forState: .Normal)        myButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)        myButton.setImage(UIImage.init(named: ""), forState: .Normal)        myButton.layer.borderWidth = 1        myButton.layer.cornerRadius = 5        myButton.layer.borderColor = UIColor.blackColor().CGColor        myButton.addTarget(self, action: #selector(self.myButtonAction(_:)), forControlEvents: .TouchUpInside)        myView.addSubview(myButton)    }    /**     myButtonAction     - parameter btn: An AlertView     */    func myButtonAction(btn:UIButton)  {        let myAlertView = UIAlertView()        myAlertView.title = "alertView"        myAlertView.message = "This is a UIAlertView"        myAlertView.addButtonWithTitle("Cancel")        myAlertView.addButtonWithTitle("Ok")        myAlertView.cancelButtonIndex = 0        myAlertView.show()    }    /**     creatAUIImageView     */    func creatAUIImageView() {        myImageView.frame = CGRectMake(10, 200, 300, 300);        myImageView.image = UIImage.init(named: "fire.jpg")        myImageView.userInteractionEnabled = true        myView.addSubview(myImageView)    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }}

打完收工,想学到更多的Swift相关的知识,欢迎关注,博主比较懒,重实用,比较难理解的概念暂时都会放放,先达到能做项目的程度。

0 0
原创粉丝点击