Swift: Constraints incorrect when device is started in landscape

I currently have an application where I programmatically set the constraints. The problem occurs when the app is started in landscape mode. When the app is started in landscape mode, the size of the UIViews width and height extend past the screen. However, when the app is started in portrait mode, the app is fine, and it keeps its constraints properly when rotating. This seems to only be a problem when opening the app in landscape. Here is my code and screen shots:

  func setUpGameBoard(){
    let width =  UIScreen.main.bounds.width
   let gameBoard = Gameboard(frame: .zero, mainVC: self)
    self.view.addSubview(gameBoard)
    gameBoard.translatesAutoresizingMaskIntoConstraints = false
    gameBoard.widthAnchor.constraint(equalToConstant: width).isActive = true
    gameBoard.heightAnchor.constraint(equalToConstant: width).isActive = true
    gameBoard.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    gameBoard.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    }

Here it is when started upright, there is no problems when rotating.


#swift #xcode

2 Likes2.55 GEEK