簡體   English   中英

iPhone - UIViewController在設備方向改變時不旋轉

[英]iPhone - UIViewController not rotating when device orientation changes

我有自己的自定義UIViewController,它包含一個帶有UIImageView的UIScrollView作為它的子視圖。 我希望在設備方向改變時使圖像自動旋轉,但它似乎不起作用......

在頭文件中,我有;

@interface MyViewController : UIViewController <UIScrollViewDelegate> {
    IBOutlet UIScrollView   *containerView;
    UIImageView *imageView;
}

這些組件在loadView函數中初始化,如下所示;

    containerView = [[UIScrollView alloc] initWithFrame:frame];

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://..."]];
    UIImage *image = [[UIImage alloc] initWithData:data];
    imageView = [[UIImageView alloc] initWithImage:image];
    [image release];

    [containerView addSubview:imageView];

我添加了以下方法,假設我需要使視圖自動旋轉...

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

MyViewController可以很好地加載我指定要從URL中獲取的圖像,並且當我翻轉設備時,正在使用正確的UIInterfaceOrientation調用shouldAutorotate ...函數。

但是,didRotateFromInterfaceOrientation方法沒有被調用,並且圖像似乎沒有自行旋轉......有人可以指出我需要添加什么,或者我在這里做錯了什么?

提前致謝!

這可能不適合你,因為你沒有指定UIViewController所在的上下文,但我剛剛在Apple文檔中發現了一個重要的問題,解釋了我遇到的類似問題。

標簽欄控制器默認支持縱向方向,除非所有根視圖控制器都支持這種方向,否則不要旋轉到橫向方向。 當發生設備方向更改時,選項卡欄控制器將查詢其視圖控制器數組。 如果其中任何一個不支持方向,則標簽欄控制器不會更改其方向。

我注意到旋轉UIView時出現問題,這不是第一個或唯一的視圖作為主窗口的直接子視圖。

因此,如果您的UIView是導航控制器或選項卡視圖控制器的一部分,您還需要覆蓋導航控制器或選項卡視圖控制器上的shouldAutoRotateToInterfaceOrientation。

另外:使用[UIApplication setStatusBarOrientation]有助於在需要手動執行時解決問題。

為了使這種事情在我的應用程序中起作用,我不得不重寫

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    [self layoutSubviews];
}

還有layoutSubviews

- (void)layoutSubviews
{
    NSLog(@"layoutSubviews called");

    ...recalc rects etc based on the new self.view.bounds...
}

我不確定這是絕對必要的,但它對我有用。

有時,如果您向視圖添加子視圖,則您有責任確保將方法傳遞給子視圖; 幾天前我寫了一篇關於此事的短文 例如,如果您有一個UIViewController並將UINavigationController添加為子視圖,則必須將此代碼添加到UIViewController如果您希望在UINavigationController.view出現時調用viewWillAppear:animated:

-(void)viewWillAppear:(BOOL)animated { 
[super viewWillAppear:animated];
[projectNavigationController viewWillAppear:animated];
}

可能是willRotateToInterfaceOrientationdidRotateFromInterfaceOrientation方法也需要由didRotateFromInterfaceOrientation調用; 我對此並不十分確定,但試一試。

這在Apple Technical Q&A QA1688中進行了討論。

有時,如果由於某種原因將多個視圖堆疊在一起,則anotherController可能不會接收旋轉事件。

[myWindow addSubview:primaryViewController.view];
[myWindow addSubview:anotherController.view];

修復此問題的一種懶惰方式(不是一個好的設計)只在窗口上添加一個子視圖,但在應用程序委托上初始化多個控制器。 然后,當您需要切換窗口時,刪除當前視圖並添加所需的視圖

[self.view removeFromSuperview];
AppDelegate *dg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[dg window] addSubview:[[dg viewController] view]]; 

如果您要從橫向旋轉到肖像,請執行此操作!

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

我從這個鏈接復制了這個

它對我有用....我在這里添加這個的原因是為了讓其他人更容易找到。 我花了很多時間才找到這個修復:

創建一組UIViewController類型的新類文件,進入該類的.h文件並更改此行

@implementation MyTabBarController: UIViewController {}
@end

這樣的事情

@implementation MyTabBarController: UITabBarController{

}

現在進入nib文件並單擊UITabBarController對象並轉到它的標識選項卡,並使其成為Class MyTabBarController。

現在在MyTabBarController.m中確保它在其中。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)io {
     return YES;
}

如果你願意,你可以在那里擺脫其他一切。

我剛遇到類似的問題。 我有一系列視圖控制器/復雜視圖,它們完全旋轉,無法弄清楚為什么我剛添加的新視圖不旋轉。 經過大量的反復試驗,原因是我在分配視圖控制器時沒有調用init方法(它是標准的init方法); 比如我在做

    m_timerViewController = [TimerViewController alloc];

代替

    m_timerViewController = [[TimerViewController alloc] init];

擴展jonoogle的帖子。 我有類似的錯誤。 我的視圖有一個筆尖和我的自定義init:

- (id)initWithCategory:(Category *)category inManagedObjectContext:context{

沒有包含對init的調用。

self = [super initWithNibName:nil bundle:nil];

添加該行使我的視圖像它應該的那樣旋轉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM