如何更改应用程序的方向而不更改iphone应用程序中的设备方向
我想在不更改iphone应用程序中的设备方向的情况下更改应用程序的方向。 我想以编程方式将我的视图从portraid模式更改为景观模式。如何更改应用程序的方向而不更改iphone应用程序中的设备方向
还想知道这是否会被苹果商店接受?
感谢
现在,我得到来自其他即是解决如下
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
,当你在那个时候一个警告出现添加此行,为消除此警告只需添加上你实现娄代码文件..
@interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)
- (void) setOrientation:(UIInterfaceOrientation)orientation;
@end
,之后在波纹管的方法,如果需要的只是写这个代码..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
但现在想知道这是否被苹果应用程序商店接受?
感谢
使用这一行编程改变方向......
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
而且当你在那个时候一个警告出现,为消除此警告只需添加上你实现文件娄代码中加入这一行..
@interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)
- (void) setOrientation:(UIInterfaceOrientation)orientation;
@end
之后,如果需要,在波纹管的方法只写这个代码..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
// return NO;
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
我希望这可以帮到你..
:)
的文档描述的方位属性作为被只读的,所以如果它的作品,我不知道,你可以依靠它在未来的工作(除非苹果做了一件聪明事,这个变化;无论用户当前如何拿着他们的设备,强制改变方向都是一个明显的功能需求)。
作为替代,插在viewDidLoad中下面的代码将成功(而且有些奇怪)力的方向(假设你已经修改你shouldAutorotateToInterfaceOrientation):
if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *view = [window.subviews objectAtIndex:0];
[view removeFromSuperview];
[window addSubview:view];
}
显然,这样做,如果用户当前将其设备以纵向方向保持(因此,您的shouldAutorotateToInterfaceOrientation
可能只设置为横向模式,如果用户将设备保持纵向模式,则此例程会将其转换为横向模式)。如果您的shouldAutorotateToInterfaceOirentation
仅设置为肖像,您只需将UIDeviceOrientationIsPortrait
与UIDeviceOrientationIsLandscape
对换即可。
出于某种原因,从主窗口中删除视图,然后重新添加它迫使它查询shouldAutorotateToInterfaceOrientation
并正确设置方向。鉴于这不是苹果认可的方法,也许应该避免使用它,但它适用于我。你的旅费可能会改变。但是这也涉及其他技术。检查 SO discussion
添加类变量
Bool isInLandsCapeOrientation;
在viewDidLoad中
使用这个标志
isInLandsCapeOrientation = false;
以下函数添加
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (!isInLandsCapeOrientation) {
return (UIInterfaceOrientationIsPortrait(interfaceOrientation));
}else {
return (UIInterfaceOrientationIsLandscape(interfaceOrientation));
}
}
从纵向改变方向为横向,让它发生在一个按钮动作
- (IBAction)changeOrientationButtonPressed:(UIButton *)sender
{
isInLandsCapeOrientation = true;
UIViewController *viewController = [[UIViewController alloc] init];
[self presentModalViewController:viewController animated:NO];
[self dismissModalViewControllerAnimated:NO];
}
这工作对我很好。
如果您想更改特定视图仅在landscape..thenü可以尝试在viewDidLoad中
float angle = M_PI/2;
CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
[ [self.view] setTransform:transform];
下面要改变方向portraid模式landscap模式使用此代码
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
使用此代码以编程方式更改方向...
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
没有改变与您的鳕鱼e,在UI中没有变化。UI应该从肖像变为景观 – uttam
只需放在你的viewWillAppear:方法的这一行的上方。[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; –
我希望在点击按钮时更改方向。而且我正在对应用程序进行回退。 – uttam