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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • iOS开发中,页面传值是很常见的,但是页面传值你究竟知道多少呢?笔者这篇文章就是给大家介绍一下页面传值的具体方式,...
    蒲公英少年带我飞阅读 6,530评论 10 45
  • VLC的集成和使用 VLC介绍 VLC Media Player (VideoLAN) 为 Windows、Lin...
    Pocket阅读 20,142评论 75 66
  • 转自:http://www.code4app.com/blog-866962-1317.html1、设置UILab...
    MMOTE阅读 5,703评论 1 1
  • 1.oc基本语法 // // main.m // oc基本语法 // // Created by lanou on...
    GOT_HODOR阅读 3,349评论 0 0
  • CDH5.12震撼发布,强势集成Hue 4,打包了很多激动人心的新功能,你能想象现在可以在Hue中直接使用Clou...
    留恋红尘阅读 8,435评论 0 1

友情链接更多精彩内容