1,各种常见位置的定位
下面使用按钮演示如何将元件居中(水平居中、垂直居中),以及放置在边角处(左上角,右上角,左下角,右下角)
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let btn1 = createButton("左上")
self.view.addSubview(btn1)
let btn2 = createButton("右上")
btn2.frame.origin.x = self.view.bounds.width - btn2.frame.width
self.view.addSubview(btn2)
let btn3 = createButton("左下")
btn3.frame.origin.y = self.view.bounds.height - btn3.frame.height
self.view.addSubview(btn3)
let btn4 = createButton("右下")
btn4.frame.origin = CGPoint(x: self.view.bounds.width - btn4.frame.width,
y: self.view.bounds.height - btn4.frame.height)
self.view.addSubview(btn4)
let btn5 = createButton("居中")
btn5.center = CGPoint(x: self.view.bounds.width /