亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

支持多種方向的最簡單方法?當應用程序處于橫向模式時,如何加載自定義NIB?

支持多種方向的最簡單方法?當應用程序處于橫向模式時,如何加載自定義NIB?

iOS
開心每一天1111 2019-11-07 10:21:26
我有一個想要支持多種方向的應用程序。我有兩個要使用的.xib文件myViewController.xib,它們myViewControllerLandscape.xib. myViewController.xib存在于project / Resources中,并且myViewControllerLandscape.xib存在于根項目目錄中。我想做的是使用單獨的NIB (myViewControllerLandscape.xib)進行旋轉。我嘗試在viewDidLoad like中檢測旋轉:if((self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)) {  NSLog(@"Landscape detected!");  [self initWithNibName:@"myViewControllerLandscape" bundle:nil]; }但是我可以在gdb中看到,當應用程序在橫向設備上啟動時,這不會執行。NSLog消息不會觸發。為什么是這樣?我做錯了什么?另外,如果我將initWithNibName函數調用顯式放入viewDidLoad方法中,則該nib不會加載,并繼續顯示myViewController.xib文件。我的電話怎么了?我應該指定捆綁商品嗎?謝謝!
查看完整描述

3 回答

?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

您必須加載一個視圖,然后檢查方向并在需要時加載另一個視圖。shouldAutorotateToInterfaceOrientation:如果要旋轉,則返回“是”來檢查方向。


我使用導航控制器來管理過渡。如果我具有人像視圖并且設備旋轉,則我推動風景視圖,然后在返回肖像時彈出風景視圖。


編輯:


我應該在shouldAutorotateToInterfaceOrientation中為所有方向返回YES,但是在應用啟動時會調用此方法嗎?您是否將視圖推入此功能?


方向常數不是您查詢的全局變量,而是系統發送給控制器的消息的一部分。因此,在加載視圖控制器之前,您無法輕松檢測方向。相反,您可以硬接線該應用程序,使其以特定的方向(通常是縱向)開始,然后立即旋轉。(請參閱移動Safari。它始終以縱向開始,然后旋轉為橫向。)


這是我用來交換人像和風景視圖的兩種方法。


所有三個視圖控制器都具有此方法:


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    // Return YES for supported orientations

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}

肖像有:


- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {


    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight) {

        [self.nav pushViewController:rightLVC animated:NO];

    }

    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {

        [self.nav pushViewController:leftLVC animated:NO];

    }

}

每個景觀控制器具有以下功能:


- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {


    if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {

        [self.nav popViewControllerAnimated:NO];

    }

該應用程序以縱向啟動。如果設備的方向為橫向,則按適當的橫向。當設備旋轉回縱向時,它將彈出風景。對于用戶而言,它看起來像是同一視圖將其自身重組為不同的方向。


查看完整回答
反對 回復 2019-11-07
?
森欄

TA貢獻1810條經驗 獲得超5個贊

我稍加修改,以允許在肖像或風景中初步裝入筆尖


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

? ? if( !nibNameOrNil )? ? ?nibNameOrNil = [self nibNameRotated:[[UIApplication sharedApplication] statusBarOrientation]];

? ? self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

? ? return self;

}


- (NSString*) nibNameRotated:(UIInterfaceOrientation)orientation

{

? ? if( UIInterfaceOrientationIsLandscape(orientation))? ? ?return [NSString stringWithFormat:@"%@-landscape", NSStringFromClass([self class])];

? ? return [NSString stringWithFormat:@"%@", NSStringFromClass([self class])];

}


-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

{

? ? NSString *nibname = [self nibNameRotated:toInterfaceOrientation];

? ? [[NSBundle mainBundle] loadNibNamed:nibname owner:self options:nil];

? ? [self viewDidLoad];

}


查看完整回答
反對 回復 2019-11-07
  • 3 回答
  • 0 關注
  • 529 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號