简体   繁体   English

UIWindow正在重置我的子视图

[英]UIWindow is reseting my subview

I have two view controllers, one of them is a subview of the other. 我有两个视图控制器,其中一个是另一个的子视图。 Both views are loaded up via nibs. 两个视图都通过笔尖加载。 The sub view controller has it position set in viewDidLoad of the parent view controller. 子视图控制器将其位置设置在父视图控制器的viewDidLoad中。 When the parent view controller is set as the root view controller to the UIWindow, it resets the position of the sub view controller's view back to {0.0, 0.0} . 当父视图控制器设置为UIWindow的根视图控制器时,它会将子视图控制器视图的位置重置为{0.0, 0.0}

I know it is the window because I decided to observe the frame of the sub view controller's view and added a breakpoint ( the observer is the parent view controller ). 我知道它是窗口,因为我决定观察子视图控制器视图的框架并添加一个断点(观察者是父视图控制器)。 The stack trace reports back with: 堆栈跟踪报告回:

#0  0x000d34e0 in -[ParentViewController observeValueForKeyPath:ofObject:change:context:]
#1  0x32866eb0 in NSKeyValueNotifyObserver ()
#2  0x32866b0a in NSKeyValueDidChange ()
#3  0x32840eca in -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] ()
#4  0x39b87d9e in -[UIViewController viewDidMoveToWindow:shouldAppearOrDisappear:] ()
#5  0x39b4075e in -[UIView(Internal) _didMoveFromWindow:toWindow:] ()
#6  0x39b405be in -[UIView(Internal) _didMoveFromWindow:toWindow:] ()
#7  0x39b3ae4a in -[UIView(Hierarchy) _postMovedFromSuperview:] ()
#8  0x39b227dc in -[UIView(Internal) _addSubview:positioned:relativeTo:] ()
#9  0x39b222c2 in -[UIView(Hierarchy) addSubview:] ()
#10 0x39b7fe42 in -[UIWindow addRootViewControllerViewIfPossible] ()
#11 0x39b7bae4 in -[UIWindow _setHidden:forced:] ()

If I'm not meant to set the position in viewDidLoad where am I meant to set it? 如果我不打算在viewDidLoad中设置位置,我打算在哪里设置它?

Update: I thought it was just the position being changed, but through the same path, the dimension are being set. 更新:我认为只是位置被改变,但是通过相同的路径,维度被设置。 In the nib, it is set at landscape dimensions, but the frame is being set to portrait dimension at {0.0, 0.0} . 在笔尖中,它设置为横向尺寸,但框架在{0.0, 0.0}处设置为纵向尺寸。

I would suggest doing it in viewWillAppear or viewDidAppear . 我建议在viewWillAppearviewDidAppear When viewDidLoad is called, the view has only been loaded into the memory and has not been drawn to the screen yet which causes such problems. 当调用viewDidLoad ,视图仅被加载到内存中并且尚未被绘制到屏幕上,这导致了这样的问题。

Trying some different search keywords, I came across a couple of links that did prove very useful: 尝试一些不同的搜索关键字,我遇到了几个确实非常有用的链接:

The first goes into further detail of what Numan Tariq was saying and the last explains what is my problem. 第一部分详细介绍了Numan Tariq所说的内容,最后解释了我的问题。

The issue is that the auto resizing masks were not set. 问题是未设置自动调整大小的蒙版。 In the Interface Builder, sometimes the view has flexible width and height and locked to all margins, and sometimes it has just the left and top margins locked. 在界面生成器中,有时视图具有灵活的宽度和高度并锁定到所有边距,有时它只有左边和上边距锁定。 For the way I am using it, I need the view to have the first. 对于我使用它的方式,我需要视图才能拥有第一个。 It now works and shows up the correct size and, thanks to Numan Tarig ;), position. 它现在可以工作并显示正确的尺寸,感谢Numan Tarig;),位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM