[英]Force set Landscape orientation is not working in xib
我有以下代碼
#define degreesToRadian(x) (M_PI * (x) / 180.0)
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
self.view.bounds = CGRectMake(0.0f, 0.0f, 1004, 768.0f);
// self.view.center = CGPointMake(160.0f, 240.0f);
[UIView commitAnimations];
}
此代碼對於強制設置橫向方向效果很好
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
我在viewdidload中使用以下代碼。此代碼在測試應用程序中正常運行,但在我的mainapp中運行正常。
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
{
objc_msgSend([UIDevice currentDevice], @selector(setOrientation:),UIInterfaceOrientationLandscapeLeft);
}
}
如果我編寫測試應用程序就可以正常工作,但是在我的Mainapp中卻無法正常工作。我的Mainapp中有xib
確保您已為Mainapp應用程序設置了橫向模式,
編輯
在Printview頁面ios5和6中使用以下方法
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsLandscape( interfaceOrientation)) {
return YES;
}
return NO;
}
對於iOS 7
-(BOOL)shouldAutorotate
{
if (UIInterfaceOrientationIsLandscape( [[UIDevice currentDevice] orientation])) {
return YES;
}
else
return NO;
}
在項目設置中,您需要檢查應用程序中應該可用的所有方向(即使僅使用一次)。
然后,在最頂層的對象(通常是(Custom)UITabBar或(Custom)UINavigationBar)上,您必須使用以下定向方法來實現一些邏輯:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- (NSUInteger)supportedInterfaceOrientations
如果您想了解更詳細的輪換教程或答案,它會認為是最好的。
我不知道您的項目是否還支持iOS5,但是該版本處理輪換的方式有所不同。
首先,您將通過.xib進行設置
然后針對特定視圖編寫此方法
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeRight;
}
對於另一個類/視圖,請為所有人編寫“方向”。
注意:-您需要在所有類中編寫這些方法。
我在didRotateFromInterfaceOrientation中編寫了以下代碼:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
UIDeviceOrientation orientaiton = [[UIDevice currentDevice] orientation];
NSLog(@"orientation %d",orientaiton);
if(UIInterfaceOrientationIsPortrait(orientaiton)){
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
{
if(orientaiton == UIInterfaceOrientationPortrait)
objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft);
else if (orientaiton == UIInterfaceOrientationPortraitUpsideDown)
objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeRight);
}
}
}
我可以旋轉到風景,但是問題是視圖旋轉到肖像然后返回到風景,但是我不想那樣,我想將視圖固定為風景。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.