Swift - UIView,UILabel,UIButton,UIImageView

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


import UIKit

class 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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingFire

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值