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
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 .
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With