Swift实现页面元素的绝对定位(水平居中,垂直居中,右上

01-01 23:17:34   来源:   评论: 点击:

页面元素的绝对定位许多朋友会想到是css定位了,做手机开发的也需要定位了,我们下面来看一篇Swift实现页面元素的绝对定位(水平居中,垂直
页面元素的绝对定位许多朋友会想到是css定位了,做手机开发的也需要定位了,我们下面来看一篇Swift实现页面元素的绝对定位(水平居中,垂直居中,右上角等等)的教程,具体的细节如下所示。

1,各种常见位置的定位

使用按钮演示如何将元件居中(水平居中、垂直居中),以及放置在边角处(左上角,右上角,左下角,右下角)

原文: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)下方开始

有时我们放置的页面元素不想与状态栏重合,而是从状态栏下方开始,那么加上状态栏高度即可。

原文: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)下方开始

同样的,如果页面上有导航栏,而元素又不想被导航栏遮住。那么将其加上导航栏的坐标和高度即可。

原文: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()
    }
}

相关热词搜索:

上一篇:安卓开发使用定制的ArrayAdapter制作ListView的Items 下一篇:ios开发之将中文转换成字符值引用 (numeric character reference, NCR)
分享到: 收藏