Swift實現頁面元素的絕對位置(水平置中,垂直置中,右上方等等)

來源:互聯網
上載者:User

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()
    }
}
相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.