Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I horizontally center a UIView?

Tags:

ios

swift

How can I make UIView horizontally centered and 30px from the top of the view controller using CGRectMake?

var draggableView: DraggableView = DraggableView(frame:CGRectMake(0 , 30, 260, 260))
draggableView.center = center
like image 857
Nurdin Avatar asked Feb 19 '15 19:02

Nurdin


People also ask

How to center storyboard?

Select your button (or whatever view you want to center) on the storyboard. Then click the align button on the bottom right. Select Horizontally in Container and Vertically in Container .

How to center a button in xcode?

To create constraints select the button and click the Align icon in the auto layout menu. A popover menu will appear, check both “Horizontal in container” and “Vertically in container” options to center the button on the screen.


1 Answers

Try:

let size = 260
let screenWidth = self.view.frame.size.width

let frame = CGRectMake((screenWidth / 2) - (size / 2), 30, size, size)
let draggableView = DraggableView(frame: frame)

self.view.addSubview(draggableView)
like image 133
nanothread Avatar answered Nov 15 '22 12:11

nanothread