Swift - 頁面元素的絕對定位(水平居中,垂直居中,右上角等等)

1,各類常見位置的定位 html

下面使用按鈕演示如何將元件居中(水平居中、垂直居中),以及放置在邊角處(左上角,右上角,左下角,右下角)swift

原文:Swift - 頁面元素的絕對定位(水平居中,垂直居中,右上角等等)

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)下方開始 有時咱們放置的頁面元素不想與狀態欄重合,而是從狀態欄下方開始,那麼加上狀態欄高度便可。ide

原文:Swift - 頁面元素的絕對定位(水平居中,垂直居中,右上角等等)

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)下方開始
一樣的,若是頁面上有導航欄,而元素又不想被導航欄遮住。那麼將其加上導航欄的座標和高度便可。code

原文:Swift - 頁面元素的絕對定位(水平居中,垂直居中,右上角等等)

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()

    }

}
相關文章
相關標籤/搜索