旋转时调整 iPhone 上的视图大小
我有一个有很多视图的应用程序。我希望只有几个视图能够在设备旋转时旋转为横向。我发现我无法使用 (BOOL)shouldAutorotateToInterfaceOrientation
因为这会旋转我的应用程序中的每个视图。 我在 Stack Overflow 上找到了这个问题的解决方案,但现在我有另一个问题需要处理。
当我转动设备时,视图会旋转,但它仍然显示视图,就好像它仍然处于纵向模式(垂直向上和向下)一样。视图的顶部和底部被切断。有没有办法让视图旋转并调整其大小以适应新的方向? 我还发现这个但无法让它发挥作用。
这是我的该视图的代码:
@implementation businessBank
@synthesize webView, activityIndicator;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlAddress = @"website_url";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
- (void)didRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[notification object] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft) {
[self.view setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)];
} else if (orientation == UIDeviceOrientationLandscapeRight) {
[self.view setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];
} else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
[self.view setTransform:CGAffineTransformMakeRotation(M_PI)];
} else if (orientation == UIDeviceOrientationPortrait) {
[self.view setTransform:CGAffineTransformMakeRotation(0.0)];
}
}
I have an application with many views. I want only a couple of the views to be able to rotate to landscape when the device is rotated. I found out that I couldn't use (BOOL)shouldAutorotateToInterfaceOrientation
because that would rotate every view in my app.
I found a solution to this problem here on Stack Overflow but now I have another issue to deal with.
The view rotates when I turn the device but it still shows the view as if it were still in portrait mode (straight up and down). The top and bottom of the view is cut off. Is there a way to have the view rotate and also adjust its size to fit the new orientation?
I also found this but wasn't able to get it to work.
Here's my code for that view:
@implementation businessBank
@synthesize webView, activityIndicator;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlAddress = @"website_url";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
- (void)didRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[notification object] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft) {
[self.view setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)];
} else if (orientation == UIDeviceOrientationLandscapeRight) {
[self.view setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];
} else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
[self.view setTransform:CGAffineTransformMakeRotation(M_PI)];
} else if (orientation == UIDeviceOrientationPortrait) {
[self.view setTransform:CGAffineTransformMakeRotation(0.0)];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您有一个非常基本的应用程序可以拉伸以匹配方向,否则自动调整大小无论如何都不适合您 - 您确实需要为两个方向创建两个单独的视图。
有关自动调整视图大小的帮助,您可以查看本教程:
http://theappleblog.com/2009/04/08/iphone-dev-sessions-how-to-make-an-orientation-aware-clock/
要使用更强大的切换视图的方法,本教程应该可以帮助您:
http://icodeblog.com/2008/08/03/iphone-programming-tutorial-transitioning- Between-views/
Unless you have a very basic app that stretches to match the orientation, auto-resizing is not going to work well for you anyways - you really need to create two separate views for the two orientations.
For help on autoresizing views, you can check this tutorial:
http://theappleblog.com/2009/04/08/iphone-dev-sessions-how-to-make-an-orientation-aware-clock/
To use the more robust method of switching views, this tutorial should help you out:
http://icodeblog.com/2008/08/03/iphone-programming-tutorial-transitioning-between-views/