当在 UITabBarController 中选择 UIViewController 时视图框架发生变化
据我所知,我遇到了一个奇怪的问题。问题是,当 UIVIewController 的视图框架在 UITabBarController 中首次显示时发生了变化。
我将尝试描述我的架构。 我有一个类是 UITabController 的子类。在这个类中,我创建了 3 个 UIViewController(A、B、C),然后将它们放入 UITabBarController 的 viewControllers 数组中。 UITabBarController 的 selectedIndex 设置为 0,因此 A 是第一个显示的。一切在视图 A 中正常渲染。但是当我切换到第二个视图 B 或第三个视图 C 时,每个子视图框架都会以某种方式损坏。尺寸和位置发生变化。
i NSLog UIViewControllers (A, B, C) 的帧,然后将它们添加到 UITabBarController 的 viewControllers 数组中,结果是 (0,20,320,460)。当它们显示时,它们的框架变为(0,0,320,411)。但 ViewControlles A 仍然呈现正常。另外两个不是:S
I'am having for my knowledge a strange problem. The thing is that frame of UIVIewController's view changes when is beeing displayed for the first time in UITabBarController.
I'll try to describe my architecture.
I have a class that is subclass of UITabController. Inside this class i create 3 UIViewController (A,B,C) and than put them to viewControllers array of UITabBarController.
selectedIndex of UITabBarController is set to 0 so A is the first one dislayed. Everthing renders normaly in view A. But when i switch to second view B or third view C every subview frame is somehow broken. Sizes and positions are changed.
i NSLog the frames of UIViewControllers (A, B, C) before adding them to the viewControllers array of UITabBarController and the result is (0,20,320,460). At the moment they are displayed, their frame changes to (0,0,320,411). But ViewControlles A still renders OK. Other two not:S
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
必须查看一些代码才能确切知道问题所在,但与此同时,您应该做的是将子视图设置为 viewController 的 viewWillAppear 方法,并使用 BOOL 来确保它们仅设置一次(而不是每次都设置) viewWillAppear 方法被调用)。这应该确保子视图匹配正确的框架。
Would have to see some code to know exactly what the problem is but in the meantime what you should do is setup the subviews int the viewWillAppear method of your viewController and use a BOOL to make sure they are only ever setup once (not every time the viewWillAppear method is called). This should make sure the subviews match the correct framing.
将视图添加到 tabBar 数组后,框架高度可能会发生变化,这确实是有意义的。标准 tabBar 的高度是 49px,这是您在日志中看到的高度值的差异。这是有道理的,因为每个视图都会调整以适应可用的屏幕尺寸,减去 tabBar 的尺寸。
您如何以编程方式或 IB 方式设置您的视图?您使用什么尺寸?如果您以编程方式或通过 IB 设置框架,但没有考虑到 tabBar 的偏移,则可能会偏离视图的放置位置。查看一些代码会有所帮助,但如果没有任何代码,我建议检查您如何调整选项卡栏的框架。
It does make sense that your frame height could change after adding the views to the tabBar array. The height of the standard tabBar is 49px, which is the difference in the height values you are seeing in your logs. This makes sense, since each view is adjusted to fit the available screen size, minus the size of the tabBar.
How are you setting up your views, programmatically or IB? What sizes are you using? If you are setting up your frames, either programmatically or through IB, but not taking into consideration the offset from the tabBar, it could throw off where your views are being placed. It would help to see some code, but without any, I'd suggest checking how you adjusted your frames for the tab Bar.