xcode 在 iOS 6 中获取当前的界面方向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13968081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting current interface orientation in iOS 6
提问by Noufal Kmc
I do the orientation checking dynamically in iOS 5.x as below:
我在 iOS 5.x 中动态进行方向检查,如下所示:
-(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;
}
For iOS 6, I did the below code but thats not working:
对于 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;
}
How can I check the interface oriention in iOS 6 just like I did in iOS 5.x?
如何像在 iOS 5.x 中那样检查 iOS 6 中的界面方向?
Thanks
谢谢
回答by Levi
You can use the viewWillLayoutSubviews
method, where you check the orientation whith
您可以使用该viewWillLayoutSubviews
方法,在其中检查方向
[[UIApplication sharedApplication] statusBarOrientation];
[[UIApplication sharedApplication] statusBarOrientation];
and set your frames accordingly.
并相应地设置您的框架。
Hope this helps!
希望这可以帮助!
回答by Nikola Kirev
Try to do the check in this method
尝试在此方法中进行检查
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
duration:(NSTimeInterval)duration
Edit:
编辑:
Use this:
用这个:
BOOL isInPortrait = UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]);
回答by Noufal Kmc
Combined Levi's and Nikola Kirev's answers and now the code is working properly. Thanks both of you. Here is the code for others to reference :
结合 Levi's 和 Nikola Kirev 的答案,现在代码运行正常。谢谢你们俩。以下是供其他人参考的代码:
For iOS 6 :
对于 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
}
}
For iOS 5.x and 4.x
对于 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;
}
}
回答by V-Xtreme
Try this :
尝试这个 :
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]