我正在开发具有如下结构的应用程序:
编辑:main.storyboard 上有一个通用的标签栏 Controller
main.Storyboard -> NavController -> (relationship) ViewControllerA -> (show/push segue) ViewControllerB
secondary.Storyboard -> NavController -> (关系)ViewControllerC
从 ViewControllerC,我需要转到 ViewControllerB。我正在使用 Storyboard 引用来执行此操作。 segue 工作,依赖项在 segue 中传递,但是我有一个程序 View ,当我从 second.Storyboard 上的 ViewControllerC 呈现 VC 时加载它并没有加载,并且在使用 Storyboard segue 呈现时正常加载
以下是我触发对 ViewControllerB 的 segue 的方式:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let someObject = fetchedResultsController.object(at: indexPath).someRepresentation
performSegue(withIdentifier: "theIdentifier", sender: someObject)
}
这是我处理segue的方式:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "theIdentifier" {
guard let destination = segue.destination as? ViewControllerB,
let sender = sender as? SomeCustomClass else { return }
destination.receivedObject = sender
}
}
我正在创建 View 并在 ViewWillAppear 中添加到 View 的 subview 。我也在 ViewDidAppear 中尝试过,没有改变效果。同样,当 ViewController 从同一个 Storyboard呈现时创建 View
这是我最终创建 View 的方式
let webNavView = WebNavToolbarView(viewLayoutMarginsGuide: view.layoutMarginsGuide)
let yPosition: CGFloat = webNavView.frame.height / 4
let xOffset: CGFloat = 8
let backButton = createButton(
image: .back,
xPosition: xOffset,
yPosition: yPosition,
selector: #selector(goBackPressed)
)
let forwardButton = createButton(
image: .forward,
xPosition: backButton.frame.maxX + xOffset,
yPosition: yPosition,
selector: #selector(goForwardPressed)
)
// FIXME: Doing this just to get the width for the createButtonMethod's xPosition parameter
let homeButton = createButton(
image: .home,
xPosition: webNavView.frame.maxX - forwardButton.frame.width - xOffset,
yPosition: yPosition,
selector: #selector(goHomeButtonPressed)
)
webNavView.translatesAutoresizingMaskIntoConstraints = false
webNavView.addSubview(backButton)
webNavView.addSubview(forwardButton)
webNavView.addSubview(homeButton)
self.view.addSubview(webNavView)
webNavView 使用传入的 layoutGuide 来获取其宽度。 X 和 Y 为 0,0
所有 View 都是在没有约束的情况下创建的(使用框架)。 createButton 方法只是在调用设置样式的初始化程序后定位按钮。
如果我打印 view.subViews,webNavView 就在那里,但它不会出现在屏幕上或 View 调试器中。它的高度和宽度都是 0,尽管它的高度被硬编码为 60
此外,如果我将工具栏添加到 parent.view,它会被添加但不尊重任何安全区域(它被放置在状态栏上)
检查 webNavView 的框架:
subview 也会以正确的位置打印出来。但同样, View 调试器中什至没有显示
主 Storyboard:
二级 Storyboard:
Storyboard引用:
最佳答案
我想我可能发现了你的问题。您说您正在使用框架布置 View ,但您正在设置:webNavView.translatesAutoresizingMaskIntoConstraints = false
.您需要摆脱这行代码,因为这会告诉编译器您正在使用自动布局来布局 View ,因此它将忽略框架。而且由于没有定义任何自动布局约束,因此您的 webview 不会显示在屏幕上。
我还会仔细检查以确保您没有将 webNavView 的 subview 的 translatesAutoresizingMaskIntoConstraints 设置为 false。
关于ios - View 不会在 ViewController 上呈现,以编程方式从不同的 Storyboard呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62379870/