Swift - UIView,UILabel,UIButton,UIImageView

简介: 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相关的知识,欢迎关注,博主比较懒,重实用,比较难理解的概念暂时都会放放,先达到能做项目的程度。

目录
相关文章
|
Swift
Swift之UIView的扩展
Swift之UIView的扩展
169 0
Swift之UIView的扩展
|
Swift
Swift - 如何让UIView,UILabel和UIImageView之间类型互相转化
Swift - 如何让UIView,UILabel和UIImageView之间类型互相转化
143 0
|
测试技术 Swift
Swift:UIButton+Extension
Swift:UIButton+Extension
206 0
|
测试技术 Swift
Swift:UIView+Extension
Swift:UIView+Extension
318 0
swift 为UIView设置投影效果
//视频标题投影 videoBackBtn.layer.shadowOffset = CGSizeMake(3,3) videoBackBtn.
1026 0
Swift学习第六枪-UIButton和UILable
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010046908/article/details/51471227 UIButton和UILable的学习 从今天开始学习基本控件,先从按钮和标签开始。
891 0
|
Swift iOS开发
【Swift】通过tag删除动态创建的UIButton
前言 最近开始做swift的基于SpriteKit的小游戏,github地址 https://github.com/diandianxiyu/CountCats ,欢迎支持! 动态创建 直接上代码 //开始按钮 let btnStart=UIButton() btnStart.
874 0