swift - iOS 13 - 右栏按钮项目在模态表演示中偏移

标签 swift xcode ios13

我在使用 iOS 13 及其新的工作表卡片样式模式演示时遇到了这个奇怪的问题。

从 ViewController1 中,我将 ViewController2 模态呈现在 NavigationController 中,一切正常。
然后,我从 ViewController2 中模态呈现嵌入到 NavigationController 中的 ViewController3,并获得右栏按钮偏移量。

这是该问题的视频:有人解决了吗?

What am I doing wrong?

主视图 Controller

import UIKit

let vc1identifier = "vc1identifier"
let vc2identifier = "vc2identifier"

class ViewController: UIViewController {

@IBAction func tap1(_ sender: UIButton) {
    if let navigation = self.storyboard?.instantiateViewController(withIdentifier: vc1identifier) as? UINavigationController,
        let nextVC = navigation.contentViewController as? UnoViewController {

        //self.navigationController?.pushViewController(nextVC, animated: true)
        self.present(navigation, animated: true, completion: nil)
    }
}
}

extension UIViewController {
var contentViewController: UIViewController {
    if let navcon = self as? UINavigationController {
        return navcon.visibleViewController!
    } else {
        return self
    }
}
}

第二个 View Controller

import UIKit

class UnoViewController: UIViewController {

@IBOutlet weak var barButton: UIBarButtonItem!


override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    barButton.title = "GotoVC2"
}

@IBAction func pressThis(_ sender: UIBarButtonItem) {
    if let navigation = self.storyboard?.instantiateViewController(withIdentifier: vc2identifier) as? UINavigationController,
        let nextVC = navigation.contentViewController as? DueViewController {

        self.present(navigation, animated: true, completion: nil)
    }
}
}

最佳答案

我也遇到了同样的问题。

解决方案很简单,你只需要告诉导航栏它需要这样的布局

 override public func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if #available(iOS 13.0, *) {
       navigationController?.navigationBar.setNeedsLayout()
    }
 }

关于swift - iOS 13 - 右栏按钮项目在模态表演示中偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58233090/

相关文章:

ios - 运行 Swift 应用时出现 Segmentation Fault 11

ios - 运行发布时出现 flutter 错误。在调试中工作正常

ios - 我不知道如何激活这个功能

ios - 在 swift 4 中实现 checkin 策略

ios - 当 UICollectionView 调用显示单元格时,数据未从 Core Data 完全加载

iOS:Tabbar - 加载选项卡的默认状态

ios - SwiftUI:ForEach 无法推断复杂的闭包返回类型

ios13 - 引用错误 : Can't find variable: IntersectionObserver

目标设置为 iOS 13 的 iOS 应用程序不会在 App Store 上提供旧版本

swift - 如何从委托(delegate)回调中刷新 Swift UIViewController