Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

ios - Can I rotate a UIView without the black bars?

I have a UIView that takes up the entirety of the window on the iphone. When it rotates to landscape, the edges get the little black bars until it snaps into place, as i would expect.

I want to make that uiview bigger than the screen, so that rotating it doesn't show those black bars but shows the parts of the view that are normally offscreen. I've tried increasing the frame and bounds of the view, but that doesn't seem to do the trick.

Has anyone done this succesfully?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to do two things to make this happen.

First, the window's root view controller will always resize its view to the size of the window. So your big view needs to be a subview of the root view controller's view (to keep it from being resized down), and your root view controller's view needs to have clipsToBounds set to NO. In fact all ancestors of the big view need to have clipsToBounds set to NO.

Second, when the window rotates, it gives itself black subviews to explicitly hide any views that would otherwise appear outside the window's bounds. It places these black subviews in front of its root view controller's view. You need to move your root view controller's view to the front, like this:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    UIWindow *window = self.view.window;
    [window bringSubviewToFront:window.rootViewController.view];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...