1, the location of a variety of common positions
Use the buttons to show how to center a component (horizontally, vertically), and place it in a corner (upper-left corner, upper-right corner, lower-left corner, lower-right corner)
| The code is as follows |
Copy Code |
| Import Uikit
Class Viewcontroller:uiviewcontroller {
Override Func Viewdidload () { Super.viewdidload ()
Let btn1 = Createbutton ("Top left") Self.view.addSubview (BTN1) Let btn2 = Createbutton ("upper right") btn2.frame.origin.x = Self.view.bounds.width-btn2.frame.width Self.view.addSubview (BTN2)
Let Btn3 = Createbutton ("bottom left") BTN3.FRAME.ORIGIN.Y = Self.view.bounds.height-btn3.frame.height Self.view.addSubview (BTN3)
Let Btn4 = Createbutton ("bottom Right") 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 ("centered") Btn5.center = Cgpoint (X:SELF.VIEW.BOUNDS.WIDTH/2, Y:SELF.VIEW.BOUNDS.HEIGHT/2) Self.view.addSubview (BTN5)
Let btn6 = Createbutton ("on") Btn6.center.x = SELF.VIEW.BOUNDS.WIDTH/2 Self.view.addSubview (BTN6)
Let btn7 = Createbutton ("Left Middle") BTN7.CENTER.Y = SELF.VIEW.BOUNDS.HEIGHT/2 Self.view.addSubview (BTN7) Let Btn8 = Createbutton ("Middle right") 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 ("Down") 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 { Create a button with a contactadd type Let Button:uibutton = UIButton (type:. Custom) Set Button size Button.frame=cgrectmake (0, 0, 80, 50) Button.frame.size = Cgsize (width:80, height:50) Set Button text Button.settitle (title, ForState:UIControlState.Normal) Button.backgroundcolor=uicolor.orangecolor () Return button }
Override Func didreceivememorywarning () { Super.didreceivememorywarning () } } |
2, the position starts from the status bar (StatusBar)
Sometimes we put the page elements do not want to coincide with the status bar, but from the bottom of the status bar to start, then add the status bar height.
| The code is as follows |
Copy Code |
Let Statusframe = Uiapplication.sharedapplication (). Statusbarframe
Let btn1 = Createbutton ("Top left") BTN1.FRAME.ORIGIN.Y = Statusframe.height Self.view.addSubview (BTN1)
Let btn2 = Createbutton ("upper right") btn2.frame.origin.x = Self.view.bounds.width-btn2.frame.width BTN2.FRAME.ORIGIN.Y = Statusframe.height Self.view.addSubview (BTN2)
Let btn6 = Createbutton ("on") Btn6.center.x = SELF.VIEW.BOUNDS.WIDTH/2 BTN6.FRAME.ORIGIN.Y = Statusframe.height Self.view.addSubview (BTN6) |
3, start at the bottom of the navigation bar (Navigationbar)
Similarly, if the page has a navigation bar, the element does not want to be obscured by the navigation bar. Then add it to the coordinates and height of the navigation bar.
| The code is as follows |
Copy Code |
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 ("Top left") BTN1.FRAME.ORIGIN.Y = margintop Self.view.addSubview (BTN1)
Let btn2 = Createbutton ("upper right") btn2.frame.origin.x = Self.view.bounds.width-btn2.frame.width BTN2.FRAME.ORIGIN.Y = margintop Self.view.addSubview (BTN2)
Let btn6 = Createbutton ("on") Btn6.center.x = SELF.VIEW.BOUNDS.WIDTH/2 BTN6.FRAME.ORIGIN.Y = margintop Self.view.addSubview (BTN6)
Let Btn3 = Createbutton ("bottom left") BTN3.FRAME.ORIGIN.Y = Self.view.bounds.height-btn3.frame.height Self.view.addSubview (BTN3)
Let Btn4 = Createbutton ("bottom Right") 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 ("centered") Btn5.center = Cgpoint (X:SELF.VIEW.BOUNDS.WIDTH/2, Y: (self.view.bounds.height + margintop)/2) Self.view.addSubview (BTN5)
Let btn7 = Createbutton ("Left Middle") Btn7.center.y = (self.view.bounds.height + margintop)/2 Self.view.addSubview (BTN7) Let Btn8 = Createbutton ("Middle right") 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 ("Down") 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 { Create a button with a contactadd type Let Button:uibutton = UIButton (type:. Custom) Set Button size Button.frame=cgrectmake (0, 0, 80, 50) Button.frame.size = Cgsize (width:80, height:50) Set Button text Button.settitle (title, ForState:UIControlState.Normal) Button.backgroundcolor=uicolor.orangecolor () Return button }
Override Func didreceivememorywarning () { Super.didreceivememorywarning () } } |