Objective c 在iOS 6中获取当前界面方向
我在iOS 5.x中动态执行方向检查,如下所示:Objective c 在iOS 6中获取当前界面方向,objective-c,ios,xcode,Objective C,Ios,Xcode,我在iOS 5.x中动态执行方向检查,如下所示: -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if(interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
// some more settings
}
else
{
self.profileNew.frame = CGRectMake(374, 540, 295, 58);
// some more settings
}
return YES;
}
对于iOS 6,我执行了以下代码,但不起作用:
-(BOOL)shouldAutorotate{
return YES;
}
-(NSInteger)supportedInterfaceOrientations
{
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
// some more settings
}
else
{
self.profileNew.frame = CGRectMake(374, 540, 295, 58);
// some more settings
}
return UIInterfaceOrientationMaskAll;
}
如何像在iOS 5.x中一样检查iOS 6中的界面方向
谢谢您可以使用
viewWillLayoutSubviews
方法,在该方法中,您可以使用
[[UIApplication sharedApplication]statusBarOrientation]代码>
并相应地设置您的帧
希望这有帮助 尝试使用此方法进行检查
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
duration:(NSTimeInterval)duration
编辑:
使用以下命令:
BOOL isInPortrait = UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]);
试试这个:
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]
结合Levi和Nikola Kirev的答案,现在代码工作正常。谢谢你们两位。以下是供其他人参考的代码:
对于iOS 6:
-(BOOL)shouldAutorotate{
return YES;
}
-(NSInteger)supportedInterfaceOrientations{
if (UIDeviceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
//other codes
}
else {
self.viewProfile.frame = CGRectMake(374, 462, 295, 58);
//other codes
}
return UIInterfaceOrientationMaskAll;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
duration:(NSTimeInterval)duration{
if (UIDeviceOrientationIsPortrait(orientation)) {
self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
//other codes
self.profileNew.frame = CGRectMake(238, 713, 295, 73);
}
else {
self.viewProfile.frame = CGRectMake(374, 462, 295, 58);
//other codes
}
}
对于iOS 5.x和4.x
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
//other codes
}
else {
self.viewProfile.frame = CGRectMake(374, 462, 295, 58);
//other codes
}
return YES;
}
}
但是恒定的UIInterfaceOrientation肖像在iOS 6中不起作用-[UIDevice orientation]
返回一个UIDeviceOrientation
,而不是UIInterfaceOrientation
您能用[UIApplication sharedApplication]statusBarOrientation]写下检查条件吗?也就是说,如果([UIApplication sharedApplication]statusBarOrientation]=“我在这里必须使用的常量”)与您的完全相同:if(UIDeviceOrientationSportRait([UIApplication sharedApplication]statusBarOrientation]){…}或者{…}
。视图willlayoutsubviews
的另一个优点是,如果您在其中更改帧,它将被设置动画。结合您和尼古拉·基列夫的回答,现在代码工作正常。谢谢你们两位。这是我的荣幸,但我是新来的,我怎么能比你们两位做得更好呢。我已经点击“是”,因为这篇文章对你有用吗?这将在应用程序启动后调用,然后在我们旋转设备时调用。我想在启动应用程序而不旋转设备时,从开始ie检查当前方向。检查[[UIDevice currentDevice]方向]不会得到正确的结果。这是高:-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)定向持续时间:(NSTimeInterval)持续时间{如果(UIDeviceOrientationSportRait(定向)){}其他{}将Levi's和您的答案结合在一起,现在代码工作正常。谢谢你们。设备方向可能未知。您需要接口方向。-[UIDevice orientation]返回UIDeviceOrientation,而不是UIInterfaceOrientationies,我想在更改设备方向时设置接口方向。它们是两个不同的枚举。将一个分配给另一个会给您意外的结果。但我这里不分配,只是检查一下。对不起,我没有和你争论。我在iOS上只有1.8年的经验,上面的代码我已经在一个应用程序中使用过了,而且工作得非常好。以下是应用程序链接: