macOS Development - Auto Layout
最近不玩王者農藥後,閒來就覺得得無聊,為打發時間,研究下 Mac 應用開發,不管是 iOS 開發,還是 Mac,正常套路下都是先學介面佈局,說到介面,那就不得不說 Auto Layout 了,下面就開搞一個最簡單的自動佈局 Demo:
-
建立 Mac 工程
預設套路,不需要解釋。
-
建立 View
override func viewDidLoad() { super.viewDidLoad() let otherView = NSView(frame: .zero) otherView.wantsLayer = true otherView.layer?.backgroundColor = NSColor.blue.cgColor self.view.addSubview(otherView) }
在 iOS 中建立 View 是用 UIView,在 Mac 中,則使用的是 NSView,其實套路很明顯,在 iOS 中是 UI 開頭的,在 Mac 下就換成 NS 開頭就可以。這裡不說全部,只說很多一部分都是這樣。看上面程式碼,Mac 下得通過 view.layer 來設定展示效果,對於和 iOS 的不同點,就得以後慢慢去積累了。
-
新增約束
class ViewController: NSViewController { override func viewDidLoad() { super.viewDidLoad() let otherView = NSView(frame: .zero) otherView.wantsLayer = true otherView.layer?.backgroundColor = NSColor.blue.cgColor otherView.translatesAutoresizingMaskIntoConstraints = false view.addConstraint(_YHLayoutConstraintMake(otherView, .left, .equal, view, .left, 10)) view.addConstraint(_YHLayoutConstraintMake(otherView, .right, .equal, view, .right, -10)) view.addConstraint(_YHLayoutConstraintMake(otherView, .top, .equal, view, .top, 10)) view.addConstraint(_YHLayoutConstraintMake(otherView, .bottom, .equal, view, .bottom, -10)) self.view.addSubview(otherView) } } @inline(__always) internal func _YHLayoutConstraintMake(_ item: AnyObject, _ attr1: NSLayoutAttribute, _ related: NSLayoutRelation, _ toItem: AnyObject? = nil, _ attr2: NSLayoutAttribute = .notAnAttribute, _ constant: CGFloat = 0, priority: NSLayoutPriority = 1000, multiplier: CGFloat = 1, output: UnsafeMutablePointer<NSLayoutConstraint?>? = nil) -> NSLayoutConstraint { let c = NSLayoutConstraint(item:item, attribute:attr1, relatedBy:related, toItem:toItem, attribute:attr2, multiplier:multiplier, constant:constant) c.priority = priority if output != nil { output?.pointee = c } return c }
這裡我們直接使用 NSLayoutConstraint 來進行自動佈局,原生的自動佈局寫法有點煩瑣,所以在使用前對它進行簡單的封閉,詳細就看上面的 _YHLayoutConstraintMake 方法,實現了這個全域性方法後,新增約束時就會顯得比較簡單了:
otherView.translatesAutoresizingMaskIntoConstraints = false view.addConstraint(_YHLayoutConstraintMake(otherView, .left, .equal, view, .left, 10)) view.addConstraint(_YHLayoutConstraintMake(otherView, .right, .equal, view, .right, -10)) view.addConstraint(_YHLayoutConstraintMake(otherView, .top, .equal, view, .top, 10)) view.addConstraint(_YHLayoutConstraintMake(otherView, .bottom, .equal, view, .bottom, -10))
這樣就可以給 otherView 新增距離 View 上下左右邊距為10的約束了。
-
最終效果
因為是自動佈局,所以不管 Window 怎麼拉伸,otherView 相對於 Window 的上下左右邊距都是10。
小結
這裡簡單地說了下 Mac 下的自動佈局,至於有沒有類似於 iOS 下的 Masnory 的庫我也不太清楚,回頭繼續學習關注下,不過,我覺得原生的在簡單封閉後,也是很好使的了。
相關文章
- 從 Auto Layout 的佈局演算法談效能演算法
- 通過Auto Layout和Size Classes深入瞭解UIStackView的好處和使用UIView
- Nessus Professional 10.7.5 Auto Installer for macOS Sonoma (updated Jul 2024)Mac
- Game Development Methodology for Small Development Teams (轉)GAMdev
- css layoutCSS
- game development -- flowGAMdev
- layout佈局
- jquery ui layoutjQueryUI
- Android Auto-Building Apps for Auto,Getting Started with AutoAndroidUIAPP
- office365 developmentdev
- What is strategy development process?dev
- BW: 80% of development costsdev
- Web Development Job in 4Webdev
- Layout Inflation :Unconditional layout, inflation from view adapterViewAPT
- Flutter layout 作弊稿Flutter
- FolderCompareFrame with good layoutGo
- flex 3 rows layoutFlex
- Scala介面Pannel、Layout
- fixed layout androidAndroid
- fixed fluid layoutUI
- Apache JMeter 5.4.1 Build DevelopmentApacheJMeterUIdev
- STM32 Hardware Developmentdev
- PL/SQL development skill testSQLdev
- Java Development Without SpringJavadevSpring
- auto型別型別
- inherit與auto
- Auto CAD
- android auto-Providing Audio Playback for AutoAndroid
- SAP Certified Development Consultant SAP NetWeaver 2004 - Application Development Focus ABAPdevAPP
- webpack 開發模式管理 DevelopmentWeb模式dev
- Development Studio 2021,dev
- C/C++ Development LibraryC++dev
- 5 Why Worry About Research and Developmentdev
- 上海聘:Sr. Development Managerdev
- ABC 322 E Product Developmentdev
- 關於 MYSQL auto_increment_offset和auto_increment_incrementMySqlREM
- mysql的auto_increment_offset和auto_increment_increment配置MySqlREM
- auto_ptr_ref和auto_ptr的關係 (轉)