UIViewController hidden behind UINavigationController but should be placed below
我正在开发一个应用程序,在该应用程序的顶部有一个 NavigationBar,并添加了一个 UIViewController 作为 RootViewController。
现在我的计划是向这个 UIViewController 添加一个新的 Subview。新的子视图也扩展了 UIViewController。我将控制器的视图(灰色矩形)添加到 UIViewController,但它位于 NavigationBar 后面。我不想要那个..所以我搜索了一个解决方案,发现了一些有趣的东西..:
当我只是将 UIView()(绿色矩形)添加到 UIViewController 时,视图的放置效果很好,因为我希望从另一个 UIViewController-View 中看到它。
我的代码如下所示:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class DashboardController: UIViewController {
var ccview:ContactCircleController! override func viewDidLoad() { let test = UIView() func setup(){ ccview.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width / 2, height: self.view.frame.width / 2) |
我已经取消选中了”扩展边缘” – 故事板上导航控制器的切换。我还向 UIViewController 添加了 edgesForExtendedLayout = [] ,对于 UIView 它工作正常。但是对于另一个 UIViewController 的视图……它没有工作。
谢谢!
如果你使用Debug View Hierarchy,你会看到你的灰色ccview.view不在导航栏后面,而是没有保持self.view.frame.width / 2的高度。
这是因为从 Storyboard 实例化的 UIViewController 的 .view 默认具有 .autoresizingMask = [],而在没有故事板的情况下实例化的 UIViewController 的 .view 具有默认的 .autoresizingMask = [.flexibleWidth, .flexibleHeight]。
您可以通过将 setup() 更改为:
来更正此问题
1
2 3 4 5 6 7 8 9 10 11 12 13 |
func setup(){
ccview = ContactCircleController() ccview.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width / 2, height: self.view.frame.width / 2) // remove this line // add this line self.view.addSubview(ccview.view) |
来源:https://www.codenong.com/46856743/