最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - View Controller has different dimensions in simulator and device - Stack Overflow

programmeradmin3浏览0评论

I have a board game app designed for iPad. At the start of each turn, if conditions are right, a user can display a popup showing a crossword-style grid. The popup is designed as a freeform XIB. I want this to be square. I worked out how to make it square in the simulator, but it's not square on an actual iPad. To make it square, I included this code in viewDidLoad:

    super.viewDidLoad()
    let screenSize = min(UIScreen.main.bounds.width,  UIScreen.main.bounds.height)
    preferredContentSize = CGSize(width: screenSize - 50, height: screenSize - 50)

This is how it displays on the simulator.

This is how it displays on the physical iPad.

I note this is a portrait-only app. Views are rotated using transforms to orient properly for each player, presumably sitting around a table with the iPad lying flat. There's no issue with the transform logic. So why do the preferredContentSize work on the simulator but not the physical device? Perhaps relevantly, the simulator is on iPadOS 18 and the device is on iPadOS 17.

This is how the XIB is presented:

private func showCrosswordViewController() {
    let crosswordViewController = CrosswordViewController(nibName: "CrosswordViewController", bundle: nil)
    crosswordViewController.delegate = self
    crosswordViewController.view.transform = .identity.rotated(by: activePlayerAngle)
    if let activePlayerHomeView = activePlayerHomeView() {
        crosswordViewController.cardViews.removeAll(keepingCapacity: true)
        for sv in activePlayerHomeView.subviews {
            if let cardView = sv as? CardView {
                cardView.cell = nil
                crosswordViewController.cardViews.append(cardView)
            }
        }
    }
    let sideCount = 12 - (playerPositions.count - 2)
    crosswordViewController.cellsPerRow = sideCount
    present(crosswordViewController, animated: true)
}

I have a board game app designed for iPad. At the start of each turn, if conditions are right, a user can display a popup showing a crossword-style grid. The popup is designed as a freeform XIB. I want this to be square. I worked out how to make it square in the simulator, but it's not square on an actual iPad. To make it square, I included this code in viewDidLoad:

    super.viewDidLoad()
    let screenSize = min(UIScreen.main.bounds.width,  UIScreen.main.bounds.height)
    preferredContentSize = CGSize(width: screenSize - 50, height: screenSize - 50)

This is how it displays on the simulator.

This is how it displays on the physical iPad.

I note this is a portrait-only app. Views are rotated using transforms to orient properly for each player, presumably sitting around a table with the iPad lying flat. There's no issue with the transform logic. So why do the preferredContentSize work on the simulator but not the physical device? Perhaps relevantly, the simulator is on iPadOS 18 and the device is on iPadOS 17.

This is how the XIB is presented:

private func showCrosswordViewController() {
    let crosswordViewController = CrosswordViewController(nibName: "CrosswordViewController", bundle: nil)
    crosswordViewController.delegate = self
    crosswordViewController.view.transform = .identity.rotated(by: activePlayerAngle)
    if let activePlayerHomeView = activePlayerHomeView() {
        crosswordViewController.cardViews.removeAll(keepingCapacity: true)
        for sv in activePlayerHomeView.subviews {
            if let cardView = sv as? CardView {
                cardView.cell = nil
                crosswordViewController.cardViews.append(cardView)
            }
        }
    }
    let sideCount = 12 - (playerPositions.count - 2)
    crosswordViewController.cellsPerRow = sideCount
    present(crosswordViewController, animated: true)
}
Share Improve this question edited 2 days ago Victor Engel asked Feb 7 at 16:21 Victor EngelVictor Engel 2,1332 gold badges26 silver badges50 bronze badges 1
  • FYI, the same dimensions are uses no matter the orientation of the physical iPad. – Victor Engel Commented Feb 7 at 16:22
Add a comment  | 

1 Answer 1

Reset to default 1

This seems to resolve my issue, I change the way it is presented to this (this method is called in response to a user tapping on the little green view visible in my original post:

private func showCrosswordViewController() {
    let crosswordViewController = CrosswordViewController(nibName: "CrosswordViewController", bundle: nil)
    crosswordViewController.delegate = self
    crosswordViewController.view.transform = .identity.rotated(by: activePlayerAngle)

 // Each player has a rectangular area on their side of the board. The active player's instance is retrieved with function activePlayerHomeView().
    if let activePlayerHomeView = activePlayerHomeView() {
        crosswordViewController.cardViews.removeAll(keepingCapacity: true)
        for sv in activePlayerHomeView.subviews {
            if let cardView = sv as? CardView {
                cardView.cell = nil
                crosswordViewController.cardViews.append(cardView)
            }
        }
    }
    let sideCount = 12 - (playerPositions.count - 2)
    crosswordViewController.cellsPerRow = sideCount
    
    let screenSize = min(UIScreen.main.bounds.width,  UIScreen.main.bounds.height)
    let vcSize = CGSize(width: screenSize - 50, height: screenSize - 50)
    crosswordViewController.modalPresentationStyle = .formSheet
    crosswordViewController.preferredContentSize = vcSize
    present(crosswordViewController, animated: true)
}
发布评论

评论列表(0)

  1. 暂无评论