1

我正在創建iOS應用程序。它在導航控制器內運行。在我最近建立的視圖中,UITextViewsUIImageView錯誤地顯示在錯誤的座標上。我相信這可能是由於UINavigationBar對象在視圖中和設備方向更改後的位置不正確

當我在界面生成器中設置了鏡像的視圖時,實際上 顯示錯誤的視圖。我再次假設這是由於UINavigationBar。我設法通過改變Interface Builder座標來「解決」這個問題,直到它正確顯示。這就是界面生成器現在的樣子:

我希望視圖可以在兩種設備方向(縱向和橫向)中工作。視圖中的對象不易被告知旋轉,所以我以編程方式定位視圖。代碼如下:

-(void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation { 
    if (orientation == UIInterfaceOrientationPortrait) { 
     appInfo.frame = CGRectMake(20, 19, 280, 90); 
     updateInfo.frame = CGRectMake(20, 117, 280, 86); 
     authorInfo.frame = CGRectMake(20, 229, 172, 200); 
     authorImage.frame = CGRectMake(190, 274, 110, 110); 
    } 
    else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { 
     appInfo.frame = CGRectMake(11, 44, 199, 124); 
     updateInfo.frame = CGRectMake(218, 53, 242, 124); 
     authorInfo.frame = CGRectMake(11, 180, 340, 90); 
     authorImage.frame = CGRectMake(350, 170, 110, 110); 
    } 
} 

-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    [self adjustViewsForOrientation:toInterfaceOrientation]; 
} 

-(void) viewWillAppear:(BOOL)animated { 
    [self adjustViewsForOrientation:self.interfaceOrientation]; 

    [super viewWillAppear:animated]; 
} 

這裏是我的主要問題: 當視圖旋轉爲橫向,對象不直接定位。它們不遵循在旋轉過程中要調用的座標中描述的內容。

我怎樣才能解決此問題:當旋轉回縱向也發生

這個問題?預先感謝您。

+1

你有任何的自動佈局中開啓這個觀點這有可能是你的幀變化而引起的自動佈局變得混亂 – gaige

+0

沒有。我已經把所有這些都關掉了,假設你的意思是界面生成器中的方向更改設置 – DGund

+1

不,我懷疑他的意思是在IB中自動設置大小。 – Rob

回答

3

你可以使用sizeWithFont:constrainedToSize來做一些複雜的事情來計算出各種文本框需要在新寬度上的大小以適應你想要放入它們的文本,但是可能更容易的是消除UILabel/UITextView和UIImageView,然後創建一個填充整個視圖的UIWebView,並使用它loadHTMLString:baseURL提供一個HTML字符串,其中包含您的副本和您的圖像在您的包中的鏈接。

更新:

的問題是,你(一)你顯然有autoResizingMasks設置;和(b)你過早地設置你的新幀(所以一旦重新定向,autoResizingMask再次調整它們)。

所以,一對夫婦的想法:

首先,你可能有自動調整大小面具在Interface Builder打開,例如是這樣的:

size inspector with auto resizing on

因此,關閉自動尺寸口罩:

size inspector with auto resizing off

其次,雖然我敢肯定你會做,在IB,僅供參考你也可以把它關閉程序:

appInfo.autoresizingMask = 0; 
updateInfo.autoresizingMask = 0; 
authorInfo.autoresizingMask = 0; 
authorImage.autoresizingMask = 0; 

第三,你可能要改變幀座標自動尺寸都有機會後SCRE控制你的控制。通常情況下,我會看到將這種幀調整推遲到didRotateFromInterfaceOrientation而不是willRotateToInterfaceOrientation的人,因爲(a)您具有新方向的座標(並且因爲導航控制器在橫向v縱向上的高度不同,所以這可能很重要可能不是你的情況);巧合的是(b)你可以補救一些無意識的自動修復面具可能引入的調整。

個人來說,我在早期版本的iOS帶到iOS 5的,後來我重新在viewWillLayoutSubviews幀,並在didRotateFromInterfaceOrientationviewWillAppear(我做這一切的程序在我的代碼)。 viewWillLayoutSubviews的iOS 5替代方案更爲出色,因爲它在旋轉動畫之前設置了幀,並使用旋轉動畫對動畫進行了動態變化,這是非常細微的變化,但非常靈活。一旦你開始注意到這樣做的應用程序,你總是想這樣做。

+0

沒有文字是沒有問題的。 'UITextViews'很好。唯一的問題是沒有任何東西移動到我指示的地點。 – DGund

+0

@DevinGund我原來的評論是基於何時我有一幀文字和圖像結合起來,我想重新佈局或重新定向,UIWebView負責處理所有這些垃圾,我不必考慮它,手動找出框架大小,當我碰巧想對我的副本進行更改等時進行Objective C編碼更改。但是,如果您真的想要以編程方式調整控件框架,請參閱上面的修改後的答案。 – Rob

+0

@DevinGund不要擊敗一個死馬重新UIWebView的考慮(因爲你不想使用一個),但UIWebViews的另一個明顯的優勢是,你可以格式化文本(粗體,斜體)和超鏈接到電話號碼,電子郵件地址等等。好的,我現在會關閉UIWebView的。 – Rob

2

調用adjustViewsForOrientation:對於willRotateToInterfaceOrientation:duration:回調做得太早。相反,請將調用調整爲adjustViewsForOrientation:放入willAnimateRotationToInterfaceOrientation:duration:回調中。從文檔:

By the time this method is called, the interfaceOrientation property is already set to the new orientation, and the bounds of the view have been changed. Thus, you can perform any additional layout required by your views in this method. 

UIViewController willAnimateRotationToInterfaceOrientation:duration:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration { 
    [self adjustViewsForOrientation:toInterfaceOrientation]; 
} 
1

正如在其他地方的意見討論,同時創造個人控制和走動他們的方向變化可以給你控制的上一個大臺階,簡單的方法是隻需創建一個UIWebView控件,並讓它考慮利用重新格式化更寬屏幕的文本。此外,一個UIWebView可以很容易地格式化文本(粗體,斜體)或添加超鏈接(電子郵件地址和電話號碼,尤其不管怎麼說,這是對的UIWebView創建HTML字符串的示例:

- (NSString *)htmlString 
{ 
    NSURL *imgUrl = [[NSBundle mainBundle] URLForResource:@"IMG_0999" withExtension:@"PNG"]; 
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]; 

    NSString *htmlBodyTemplate = 
     @"<html>" 
     @"<style type=\"text/css\">" 
     @"html {" 
     @" -webkit-text-size-adjust: none; /* Never autoresize text */" 
     @"}" 
     @"</style>" 
     @"<body style=\"font-family:Verdana, Geneva, sans-serif; font-size:14px;\">" 

     @"<p>This application is version %@. The recommended iOS version for running this application is iOS 5.0+. The minimum iOS version is iOS 4.3.</p>" 
     @"<p>This application will automatically check for updates at startup. It is recommended to immediately update the app when prompted.</p>" 
     @"<img src=\"%@\" style=\"float:right;\" width=\"150px\" />" 
     @"<p>This application was created by Ridgefield High School student <strong>Devin Gund</strong> in 2012. He worked with the Ridgefield Public Schools technology department in publishing this application.</p>" 

     @"</body>" 
     @"</html>"; 

    return [NSString stringWithFormat:htmlBodyTemplate, version, [imgUrl relativeString]]; 
} 

然後,在viewDidLoad中,包括行:

[self.webView loadHTMLString:[self htmlString] baseURL:nil]; 
+0

謝謝!我將來會使用它 – DGund

相關問題