我有一个iPhone应用程序,由一个显示网站的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"]];
}
}
我怎样才能做到这一点?如果你能给我代码并指出放在哪里,那将是一个很好的帮助:)
答案 0 :(得分:3)
在视图控制器中覆盖其中一个方法并将代码放在那里。
– 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"]];
}
}