当前位置: > > > Swift - 页面元素的绝对定位(水平居中,垂直居中,右上角等等)

Swift - 页面元素的绝对定位(水平居中,垂直居中,右上角等等)

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 / 2,
            y: self.view.bounds.height / 2)
        self.view.addSubview(btn5)
        
        let btn6 = createButton("上中")
        btn6.center.x = self.view.bounds.width / 2
        self.view.addSubview(btn6)
        
        let btn7 = createButton("左中")
        btn7.center.y = self.view.bounds.height / 2
        self.view.addSubview(btn7)
        
        let btn8 = createButton("右中")
        btn8.frame.origin.x = self.view.bounds.width - btn8.frame.width
        btn8.center.y = self.view.bounds.height / 2
        self.view.addSubview(btn8)
        
        let btn9 = createButton("下中")
        btn9.center.x = self.view.bounds.width / 2
        btn9.frame.origin.y = self.view.bounds.height - btn9.frame.height
        self.view.addSubview(btn9)
    }
    
    func createButton(title: String) -> UIButton {
        //创建一个ContactAdd类型的按钮
        let button:UIButton = UIButton(type:.Custom)
        //设置按钮大小
        //button.frame=CGRectMake(0, 0, 80, 50)
        button.frame.size = CGSize(width: 80, height: 50)
        //设置按钮文字
        button.setTitle(title, forState:UIControlState.Normal)
        button.backgroundColor=UIColor.orangeColor()
        return button
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

2,定位从状态栏(statusBar)下方开始
有时我们放置的页面元素不想与状态栏重合,而是从状态栏下方开始,那么加上状态栏高度即可。
let statusFrame = UIApplication.sharedApplication().statusBarFrame

let btn1 = createButton("左上")
btn1.frame.origin.y = statusFrame.height
self.view.addSubview(btn1)

let btn2 = createButton("右上")
btn2.frame.origin.x = self.view.bounds.width - btn2.frame.width
btn2.frame.origin.y = statusFrame.height
self.view.addSubview(btn2)

let btn6 = createButton("上中")
btn6.center.x = self.view.bounds.width / 2
btn6.frame.origin.y = statusFrame.height
self.view.addSubview(btn6)

3,定位从导航栏(navigationBar)下方开始
同样的,如果页面上有导航栏,而元素又不想被导航栏遮住。那么将其加上导航栏的坐标和高度即可。
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        var marginTop:CGFloat = 0
        if let rect = self.navigationController?.navigationBar.frame {
            marginTop = rect.height + rect.origin.y
        }
        
        let btn1 = createButton("左上")
        btn1.frame.origin.y = marginTop
        self.view.addSubview(btn1)
        
        let btn2 = createButton("右上")
        btn2.frame.origin.x = self.view.bounds.width - btn2.frame.width
        btn2.frame.origin.y = marginTop
        self.view.addSubview(btn2)
        
        let btn6 = createButton("上中")
        btn6.center.x = self.view.bounds.width / 2
        btn6.frame.origin.y = marginTop
        self.view.addSubview(btn6)
        
        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 / 2,
            y: (self.view.bounds.height + marginTop) / 2)
        self.view.addSubview(btn5)
        
        let btn7 = createButton("左中")
        btn7.center.y = (self.view.bounds.height + marginTop) / 2
        self.view.addSubview(btn7)
        
        let btn8 = createButton("右中")
        btn8.frame.origin.x = self.view.bounds.width - btn8.frame.width
        btn8.center.y = (self.view.bounds.height + marginTop) / 2
        self.view.addSubview(btn8)
        
        let btn9 = createButton("下中")
        btn9.center.x = self.view.bounds.width / 2
        btn9.frame.origin.y = self.view.bounds.height - btn9.frame.height
        self.view.addSubview(btn9)
    }
    
    func createButton(title: String) -> UIButton {
        //创建一个ContactAdd类型的按钮
        let button:UIButton = UIButton(type:.Custom)
        //设置按钮大小
        //button.frame=CGRectMake(0, 0, 80, 50)
        button.frame.size = CGSize(width: 80, height: 50)
        //设置按钮文字
        button.setTitle(title, forState:UIControlState.Normal)
        button.backgroundColor=UIColor.orangeColor()
        return button
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
评论0