Adding ViewController's View as subview to UIP

2020-02-16 03:18发布

问题:

I have been working on a project where I needed to show list of images with zoom / swipe feature as presented view modal. I created custom ImageViewer using ContainerView in which I have added UIPageViewController. And on demand, I added ImageViews over the UIPageViewcontroller's view. When user taps to imageView, the top UINavigation gets shown and hidden on toggle basis.

Everything worked as expected in iOS 7.1 and less. However when I tested the functionality in iOS 8 devices and simulator, the ImageViews were not added to UIPageViewController in TopLeft ( beneath UINavigationBar ). It is added below the NavigationBar as shown in bug_iOS_8.png below.

Once I touch the buggy view, it repositions itself to correct position as in image expected.png below.

I have created and tested the issue in sample project and it seems it is bug in iOS 8 itself with UIPageViewController. I went through couple of questions regarding weird behavior of UIPageViewController too. Please check the sample app here and kindly let me know if anyone has any hints on what is going on.

Thank you for your time and help.

PS: BTW I am using following version of Xcode.

Update 1:

I have tried as per the pin suggestions. However, the problem aligning is with the main view of UIViewController rather than its subview. In below image, Yellow is main View of UIViewController and red one is added subview, I added pin to "red" on in reference to superview "yellow" one. Please check following screen shots.

On startup.

After touch on screen.

Kind Regards,

回答1:

check in storyboard for particular viewController and make sure Extended Edges >> Under top bars is not selected.



回答2:

I had a very similar issue and the way i fixed it was select the view being added to the UIPageViewController in the storyboard and selected the pin options. Below in the image you can see the Constrain to margins options, make sure you unselect that. Also when choosing what view to pin it to make sure you select the superview and not the Top Layout Guide.



回答3:

Curious are you hiding your nav controller like this?

[self.navigationController setNavigationBarHidden:YES];

also if you have multiple Nav controllers, you might need to check you are referencing the correct one.

If you want it to be hidden when you present the ViewController you should move it to

    -(void) ViewWillAppear{
}