Ios 背景图片是;“闪烁”;当屏幕方向改变时
当方向更改时,我想更改视图控制器的背景图片。我在shouldAutorotateToInterfaceOrientation中调用setupGUIForOrientation函数,如下所示:Ios 背景图片是;“闪烁”;当屏幕方向改变时,ios,orientation,Ios,Orientation,当方向更改时,我想更改视图控制器的背景图片。我在shouldAutorotateToInterfaceOrientation中调用setupGUIForOrientation函数,如下所示: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { [self setupGUIForOrientation:interfaceOrientation];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
[self setupGUIForOrientation:interfaceOrientation];
return YES;
}
setupGUIForOrientation功能:
-(void)setupGUIForOrientation:(UIInterfaceOrientation)orientation
{
if(!background) background = [[UIImageView alloc] init];
UIImage* image = [[StorageProvider storage].imageProvider getImageForSurfaceElement:@"background"];
int width = 0;
int height = 0;
CGRect bounds = [[UIScreen mainScreen] bounds];
if(UIInterfaceOrientationIsPortrait(orientation)) {
width = MIN(bounds.size.width, bounds.size.height);
height = MAX(bounds.size.width, bounds.size.height);
} else {
width = MAX(bounds.size.width, bounds.size.height);
height = MIN(bounds.size.width, bounds.size.height);
}
background.frame = CGRectMake(0, 0, width, height);
[self.view addSubview: background];
[self.view sendSubviewToBack: background];
[background setImage:image];
}
一切都很好(图像和帧发生了变化),除了一件事:当旋转发生时,我可以看到视图控制器的背景色一秒钟,这真的很难看。我能阻止它吗?我可以用更好的方式编写这个函数吗?谢谢你的帮助 我猜,但您可能更新视图太早了。您没有在
shouldAutorotateToInterfaceOrientation
中调用您的方法,而是尝试在DidRotateFrominerFaceOrientation
中执行此操作吗
即,进行此更改:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self setupGUIForOrientation:self.interfaceOrientation];
}
太晚了,我可以在图片更改之前看到旧图片和背景色的“半”屏幕:SOK,然后你可以尝试willRotateToInterfaceOrientation(并使用传入的方向——self.interfaceOrientation尚未更新)。不幸的是,它做了原来的事情:感谢尝试帮助我!我找到了一个解决方案,我添加了以下代码行:[self.view setBackgroundColor:[uicolorWithPatternImage:[[StorageProvider storage].imageProvider getImageForSurfaceElement:@“background”]];进入viewDidLoad方法。它不能解决问题,但不再丑陋:)