Rectangle 27 32

How to Center a View on the Storyboard

This answer is for people who are just doing a general search for how to center things using a storyboard and don't understand constraints.

I assume you are using a Storyboard (not XIB files) and Auto Layout. This is the default nowadays so if you don't know what that means then don't worry about it. If you want to check, though, you can click Main.storyboard in the Project Navigator and then the File inspector. You can make sure that Use Auto Layout is checked:

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". Click "Add 2 Constraints".

If it wasn't perfectly centered already you may need to do one more thing. Click the "Update Frames" button that is two to the left of the align button. Your view should now be centered on the storyboard.

And more importantly, now when you run your app it should be centered no matter what device size you are using.

I originally learned this from Creating the user interface Auto layout. The basic idea is still the same, but the UI arrangement has changed a little in Xcode now.

I realize that this does not answer your question. You were right to accept @Fogmeister's answer, not mine. However, because of your title many people come here expecting to learn how to center something on a storyboard. It is those people I am trying to help.

iOS - Using storyboard and autolayout to center the UIScrollView - Sta...

ios uiscrollview autolayout storyboard
Rectangle 27 17

When using constraints, you never want to set a view's frame directly. Instead, reconfigure the constraints and let the auto layout engine set your view's frame for you. Create an IBOutlet in your view controller to store a reference to your "Center Y Alignment constraint". Make sure you connect it in your storyboard or xib.

@IBOutlet var yConstraint: NSLayoutConstraint

Then you can set the constraint's constant property to offset it from the Y position (positive number will move it down, negative moves it up).

yConstraint.constant = 200

This would move your view 200 points down.

How to move a UIView programmatically in Swift - Stack Overflow

swift uiview move
Rectangle 27 1

Create an IBOutlet in your view controller to store a reference to your "Center Y Alignment constraint". Make sure you connect it in your storyboard.

You can set the constraint's constant property to offset it from the Y position (positive number will move it down, negative moves it up).

@IBOutlet var yConstraint: NSLayoutConstraint
  yConstraint.constant = -200

This would move your view 200 points up.

That's it. Now you can change it with the help of constraints.

Move UIView Programmatically in iOS Swift - Stack Overflow

ios swift uiview
Rectangle 27 2

@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var tableHeight: NSLayoutConstraint!

override func viewDidLoad() {
    super.viewDidLoad()
    //Set table height to cover entire view
    //if navigation bar is not translucent, reduce navigation bar height from view height
    tableHeight.constant = self.view.frame.height-64
    self.tableView.isScrollEnabled = false
    //no need to write following if checked in storyboard
    self.scrollView.bounces = false
    self.tableView.bounces = true
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 20
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let label = UILabel(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 30))
    label.text = "Section 1"
    label.textAlignment = .center
    label.backgroundColor = .yellow
    return label
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = "Row: \(indexPath.row+1)"
    return cell
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView == self.scrollView {
        tableView.isScrollEnabled = (self.scrollView.contentOffset.y >= 200)
    }

    if scrollView == self.tableView {
        self.tableView.isScrollEnabled = (tableView.contentOffset.y > 0)
    }
}

swift - How to Make the scroll of a TableView inside ScrollView behave...

swift uitableview uiscrollview uikit uipageviewcontroller
Rectangle 27 0

In storyboard, drag and drop a view controller. The view controller come with a main view. Select that main view by clicking outside of the added view controller and then clicking in the center of it. Now, just drag a uiview or whatever into that main view just like you did with IB.

That's what I did in the 2nd approach. But I can't see the added view.

ios - Add subview from a xib or another scene with storyboard - Stack ...

ios view storyboard