iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 16
0

前言:今天來簡單介紹如何使用程式碼來編寫 UI

Bulid UIView

AppDelegate.swift

--


Bulid UITableView


Autolayout

use "NSLayoutConstraint"

我們這邊連 Autolayout 都用 code 來寫~

  newView.translatesAutoresizingMaskIntoConstraints = false
  
  let horizontalConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutConstraint.Attribute.centerX, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.centerX, multiplier: 1, constant: 0)
  
    let verticalConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutConstraint.Attribute.centerY, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.centerY, multiplier: 1, constant: 0)
    
    let widthConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100)
    
    let heightConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100)
    
    view.addConstraints([horizontalConstraint, verticalConstraint, widthConstraint, heightConstraint])


上一篇
照片 != 照騙
下一篇
iOS 開發有哪些常用的"庫"
系列文
iOS 開發:燃燒吧! 從初心者到超級初心者的轉職之旅30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言