Swift3.0簡(jiǎn)單UI控件的基本使用

首先介紹一下文件的結(jié)構(gòu)

class ViewController: UIViewController {

    //聲明成員變量
    override func viewDidLoad() {
        super.viewDidLoad()
    //代碼實(shí)現(xiàn)
        // Do any additional setup after loading the view, typically from a nib.
    }
    //代碼調(diào)用的函數(shù)
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
注意:Swift代碼中會(huì)出現(xiàn)"!"和"?",在此不述,你可以去看看大神們寫的
//UILabel
        let label = UILabel(frame: CGRect(x: 50, y: 50, width: 200, height: 50))
        label.text = "你好,斯威夫特"
        label.textColor = UIColor.blue
        label.textAlignment = .center
        label.backgroundColor = UIColor.red
        label.font = UIFont.systemFont(ofSize: 30)
        self.view.addSubview(label)
//UIButton的使用
       let btn = UIButton(type: UIButtonType.system)
        btn.frame = CGRect(x:50, y: 100, width: 100, height: 50)
        btn.backgroundColor = UIColor.gray
        btn.setTitle("按鈕", for: UIControlState.normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 30)
        btn.setTitleColor(UIColor.red, for: UIControlState.normal)
        btn.layer.cornerRadius = 15
        btn.addTarget(self, action:#selector(btnAction1(sender:)), for: .touchUpInside)
        self.view?.addSubview(btn)
 //button Action
    func btnAction1(sender:UIButton){
        print("點(diǎn)擊了")
//UIAlertController的使用
        let alert = UIAlertController(title: "警告", message:nil , preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "確定", style: .default, handler: nil))
        alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: { (UIAlertAction) in
            print("取消了")
        }))
        
        self.present(alert, animated: true, completion: nil)
    }
//UIImageView
let imageV = UIImageView(frame: CGRect(x: 0, y: KScreenHeight-KScreenWidth, width: KScreenWidth, height: KScreenWidth))
        imageV.image = UIImage.init(named: "123.jpeg")
        self.view.addSubview(imageV)
//UITextView
        let textV = UITextView(frame: CGRect(x:150, y: 100, width: 100, height: 50))
        textV.textColor = UIColor.white
        textV.backgroundColor = UIColor.black
        self.view.addSubview(textV)
//UITextField
        let textF = UITextField(frame: CGRect(x:250, y: 50, width: 100, height: 50))
        textF.textColor = UIColor.black
        textF.placeholder = "輸入"
        textF.borderStyle = .line
        self.view.addSubview(textF)

 //UISlider
        let slider = UISlider(frame: CGRect(x:100, y: 150, width: 100, height: 50))
        self.view.addSubview(slider)
 //要開(kāi)啟 NSAllowsArbitraryLoads為yes,不然存在報(bào)錯(cuò)可能
 //UIWebView
        let webView = UIWebView(frame: self.view.bounds)
        let url = NSURL(string:"https://www.baidu.com")
        let request = NSURLRequest(url: url! as URL)
        webView.loadRequest(request as URLRequest)
        self.view.addSubview(webView)
//UITableView
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
    var tableView : UITableView?
    var dataA = ["北京","上海","深圳","廣州","天津","南京","杭州","武漢"]
    override func viewDidLoad() {
        super.viewDidLoad() 
        self.createTableView()
    }
    func createTableView(){
        self.tableView = UITableView.init(frame:self.view.bounds,style:.plain)
        self.tableView?.delegate = self
        self.tableView?.dataSource = self
        self.view.addSubview(self.tableView!)
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataA.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "cell"
        let cell = UITableViewCell.init(style:.default, reuseIdentifier: identifier)
        cell.textLabel?.text = dataA[indexPath.row]  
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //取消選中狀態(tài)
        tableView.deselectRow(at: indexPath, animated: true)
        let str = dataA[indexPath.row]
        print("用戶選擇了\(str)")
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

//UICollectionView
//可以不簽訂layout

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource{
    let KScreenWidth = UIScreen.main.bounds.size.width
    let KScreenHeight = UIScreen.main.bounds.size.height
   
    var collectionV : UICollectionView?
    override func viewDidLoad() {
        super.viewDidLoad()
       
        self.createCollection()
    }

    func createCollection(){
    //設(shè)置Layout
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical//滾動(dòng)方向
    layout.itemSize  = CGSize(width: (KScreenWidth - 30)/2, height: 80)
    layout.sectionInset = UIEdgeInsetsMake(24, 0, 0, 0);
        
    //collection
    self.collectionV = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
    collectionV?.dataSource = self
    collectionV?.delegate = self
    collectionV?.backgroundColor = UIColor.white
    collectionV?.register(UICollectionViewCell().classForCoder,   forCellWithReuseIdentifier: "item");
    self.view.addSubview(collectionV!)
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        return 60
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "item", for: indexPath);
        
        cell.backgroundColor = UIColor.red
        
        return cell
        
    }
   
    //UICollectionViewDelegateFlowLayout
//    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
//        return UIEdgeInsetsMake(5, 10, 5, 10)
//    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末呜呐,一起剝皮案震驚了整個(gè)濱河市糯彬,隨后出現(xiàn)的幾起案子灾挨,更是在濱河造成了極大的恐慌囊扳,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,599評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件沽损,死亡現(xiàn)場(chǎng)離奇詭異令哟,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)炬太,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,629評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)驯耻,“玉大人亲族,你說(shuō)我怎么就攤上這事炒考。” “怎么了霎迫?”我有些...
    開(kāi)封第一講書(shū)人閱讀 158,084評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵斋枢,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我知给,道長(zhǎng)瓤帚,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,708評(píng)論 1 284
  • 正文 為了忘掉前任涩赢,我火速辦了婚禮戈次,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘筒扒。我一直安慰自己怯邪,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,813評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布霎肯。 她就那樣靜靜地躺著擎颖,像睡著了一般。 火紅的嫁衣襯著肌膚如雪观游。 梳的紋絲不亂的頭發(fā)上搂捧,一...
    開(kāi)封第一講書(shū)人閱讀 50,021評(píng)論 1 291
  • 那天,我揣著相機(jī)與錄音懂缕,去河邊找鬼允跑。 笑死,一個(gè)胖子當(dāng)著我的面吹牛搪柑,可吹牛的內(nèi)容都是我干的聋丝。 我是一名探鬼主播,決...
    沈念sama閱讀 39,120評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼工碾,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼弱睦!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起渊额,我...
    開(kāi)封第一講書(shū)人閱讀 37,866評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤况木,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后旬迹,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體火惊,經(jīng)...
    沈念sama閱讀 44,308評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,633評(píng)論 2 327
  • 正文 我和宋清朗相戀三年奔垦,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了屹耐。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,768評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡椿猎,死狀恐怖惶岭,靈堂內(nèi)的尸體忽然破棺而出寿弱,到底是詐尸還是另有隱情,我是刑警寧澤俗他,帶...
    沈念sama閱讀 34,461評(píng)論 4 333
  • 正文 年R本政府宣布脖捻,位于F島的核電站,受9級(jí)特大地震影響兆衅,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜嗜浮,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,094評(píng)論 3 317
  • 文/蒙蒙 一羡亩、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧危融,春花似錦畏铆、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,850評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至蛋勺,卻和暖如春瓦灶,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背抱完。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,082評(píng)論 1 267
  • 我被黑心中介騙來(lái)泰國(guó)打工贼陶, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人巧娱。 一個(gè)月前我還...
    沈念sama閱讀 46,571評(píng)論 2 362
  • 正文 我出身青樓碉怔,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親禁添。 傳聞我的和親對(duì)象是個(gè)殘疾皇子撮胧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,666評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容