在启动应用程序时无法检测设备的方向以创建UIWebView的大小正确
如果我启动我的应用程序,我想知道设备的方向。 在我的主view controller
我有一个UIWebView
,我编程添加到主view controller
。在启动应用程序时无法检测设备的方向以创建UIWebView的大小正确
我用(int)[[UIApplication sharedApplication] statusBarOrientation]
对它进行了测试,但是我只能得到数字1.无论我的设备是纵向还是横向。
我想设置我的尺寸UIWebView
但如果我不知道方向如何设置它的大小?
2016-01-21 17:21:45.387 iPhoneApp[1026:151720] 0) Orientation: 1
2016-01-21 17:21:45.388 iPhoneApp[1026:151720] 0) Orientation: 0
2016-01-21 17:21:45.393 iPhoneApp[1026:151720] 1) Orientation: 1
2016-01-21 17:21:45.393 iPhoneApp[1026:151720] 1) Orientation: 0
2016-01-21 17:21:45.406 iPhoneApp[1026:151720] 2) Orientation: 1
2016-01-21 17:21:45.406 iPhoneApp[1026:151720] 2) Orientation: 1
2016-01-21 17:21:45.417 iPhoneApp[1026:151720] 3) Orientation: 1
2016-01-21 17:21:45.417 iPhoneApp[1026:151720] 3) Orientation: 1
0是didFinishLaunchingWithOptions
1是viewDidLoad
2是viewDidAppear
3是我的方法,我创造我UIWebView
NSLog(@"3) Orientation: %d", (int)[[UIApplication sharedApplication] statusBarOrientation]);
NSLog(@"3) Orientation: %d", (int)[[UIDevice currentDevice] orientation]);
这总是我的日志的顺序。
,我创建的方法我UIWebView
看起来像
-(void)createWebView {
//NSLog(@"createWebView");
//NSLog(@"orientation: %d", [[self orientNr] intValue]);
[self destroyWebView];
NSLog(@"3) Orientation: %d", (int)[[UIApplication sharedApplication] statusBarOrientation]);
NSLog(@"3) Orientation: %d", (int)[[UIDevice currentDevice] orientation]);
if(([self firstLoad] && [[self orientNr] isEqualToNumber:[[NSNumber alloc] initWithInt:2]]) ||
([self firstLoad] && [[self orientNr] isEqualToNumber:[[NSNumber alloc] initWithInt:1]] && UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])))
[self setWebView:[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)]];
else
[self setWebView:[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]];
dispatch_async(dispatch_get_main_queue(), ^{
[[self view] addSubview:[self webView]];
});
[[self webView] setScalesPageToFit:YES];
[[self webView] setDelegate:self];
[[self webView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[[self view] setAutoresizesSubviews:YES];
//NSLog(@"iPhoneApp: %@", [self webView]);
}
变量firstLoad
检查如果应用程序尚未启动。是一个将应用程序旋转到我想要的给定方向的数字,对解决方案不重要(1代表设备,2代表横向)。 webView
是UIWebView
的类型。
因此,如果我以纵向方式启动应用程序和设备,我想创建320宽度和568高度的webView
。如果它在横向启动,我想创建宽度为568和高度为320的webView
。
我的问题是在启动时检测到我的设备的方向。我还搜查,发现了一些类似的线程(Get launch orientation of iPad app或iOS: Device orientation on load),但它并没有解决我的问题
编辑:
,如果我有我的设备上的风景或UIWebView
应在景观在推出开始时才会发生应用程序
编辑2:
问题解决。我只加
-(void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[[self webView] setFrame:[[self view] bounds]];
}
和我UIWebView
始终像我UIView
大小相同。也在横向方向。 这里的答案(https://*.com/a/24398249/5629933)解决了我的问题。我是Objective-C的新手,但确实有意义,尽管我在创建时将我的UIWebView
的大小设置为与我的UIView
相同?
您的createWebView
方法中的大部分代码都是不需要的。最重要的是在添加Web视图之前设置autoresizingMask
。
-(void)createWebView {
//NSLog(@"createWebView");
self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
self.webView.scalesPageToFit = YES;
self.webView.delegate = self;
self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.webView];
}
您的整个createWebView
方法是不明智的。你根本不应该关心方向。只需将您的网络视图添加到self.view
并使用自动布局进行定位。这样,当布局完成后,它将处于正确的位置/大小,而不管方向如何或self.view
可能发生的其他任何事情。
你正在做的一些事情太早了,这就是为什么你得到无用的结果。但是你根本不需要知道设备的方向。使用自动布局来定位您的网页视图。这样,无论发生什么事,它都会处于正确的位置。 – matt
啊好吧我明白,但大小设置不正确。如果您可以在我的方法中看到我创建“网页视图”,则其大小始终为320宽度和568高度。想象一下,如果这个视图的方向设置为横向,我的“网页视图”方向正确,但尺寸不适合整个屏幕。同样的问题,如果我的设备处于横向模式只有我的网站的大小不适合整个屏幕,因为我的'UIView'的宽度总是320宽度和568高度@matt。我如何解决它? – Premox
该截图没有意义。什么是'self.view'?什么部分应该是网络视图?网页加载到网页视图的哪一部分? – rmaddy