我有一个 iPhone 应用程序,由一个显示网站的 UIWebView 组成。在 UIWebView 在我的 ViewController.m
文件中完成加载后,我已将背景图像设置为 UIWebView,如下所示:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Set UIWebView Background Image
self->webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ui-background-portrait.png"]];
}
当设备处于纵向方向时这很好,但如果用户将设备方向切换为横向,我想更改背景图像,如果再次切换则返回纵向,等等。
我编写了下面的代码,但我不确定将其放在哪里(因为每当它们切换方向时它都需要更改背景图像;而不仅仅是一次):
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Set UIWebView Background Image
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
// code for Portrait orientation
self->webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ui-background-portrait.png"]];
}
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
// code for landscape orientation
self->webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ui-background-landscape.png"]];
}
}
我怎样才能做到这一点?如果您能给我代码并指出将其放在哪里,那将是一个很大的帮助:)
最佳答案
在 View Controller 中重写这些方法之一并将代码放在那里。
– willRotateToInterfaceOrientation:duration:
– willAnimateRotationToInterfaceOrientation:duration:
– didRotateFromInterfaceOrientation:
将此代码放入 ViewController.m
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation
{
// Set UIWebView Background Image
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
// code for Portrait orientation
self->webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ui-background-portrait.png"]];
}
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
// code for landscape orientation
self->webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ui-background-landscape.png"]];
}
}
关于XCode 通过方向更改更改背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11452328/