2014-08-29 175 views
14

我在iOS 8上運行我們的應用程序時遇到與UIAlertView相關的問題。 我顯示標題爲nil的警報。它在iOS 7中工作正常,但現在UI看起來很奇怪。在UIAlertView iOS 8 beta 5中無標題的UI問題

我在這裏附上截圖。

enter image description here 我發現的一個解決方案是,當我提供空字符串@「」它看起來沒問題。看下面的截圖。但我不確定我提到的問題是測試版iOS 8版本中的錯誤,還是有其他更好的解決方案。即使有解決它的不準確,因爲它在iOS的7

enter image description here

的iOS 7 - 顯示與標題零警報視圖。這裏截圖。 enter image description here

回答

8

它一直是最好的做法,我使用initWithTitle:@""UIAlertViewUIActionSheet因爲iOS 6中,因爲我那段時間面臨的一個設計問題,我用initWithTitle:nil時。我試圖找回來,我找不到原因究竟是什麼。

從在iOS 8的屏幕截圖,我覺得這是視圖層次對UIAlertView中爲iOS 8的變化,我認爲Auto layout可能在視圖hierarachy實施,以及你可以看到messageLabel跳起來標題標籤。

我無法確定,因爲UIAlertView的視圖層次結構是私有的。

UIAlertView類旨在按原樣使用,並且不支持子類化 。該類的視圖層次結構是私有的,並且不得修改 。

參見:https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html

但是,我使用的代碼: -

NSLog(@"%@",[self.alertView description]); 

結果在iOS 7.1:在iOS 8

<UIAlertView: 0x7fb3c05535b0; frame = (18 263; 284 62); opaque = NO; layer = <CALayer: 0x7fb3c0519810>> 

結果。0:

<UIAlertView: 0x7bf64840; frame = (0 0; 0 0); layer = <CALayer: 0x7bf648f0>> 

我不知道爲什麼的iOS 8 UIAlertView中幀(0 0 0 0);

像邁克說,我想你應該學會使用UIAlertController爲iOS 8

11

我能與iOS 8中得到的最接近的是通過設置標題的消息,而不是:但是

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location field required." message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

UIAlertView iOS8 title set

應該指出的是,UIAlertView在iOS 8的棄用如果你打算使用iOS 7和iOS 8的單獨代碼路徑,則應該使用UIAlertController代替:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Location field required." message:nil preferredStyle:UIAlertControllerStyleAlert]; 
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
}]]; 

[self presentViewController:alert animated:YES completion:nil]; 

我用兩種方法都得到了相同的結果。

4

我設法得到體面的消息隊列不加粗字體:

  1. 標題設置爲@」 「而不是零,並且
  2. (如果IOS8)在消息前加上」\ n「。
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" 
            message:@"\nLocation field required." 
            delegate:nil 
            cancelButtonTitle:@"OK" 
            otherButtonTitles:nil]; 

enter image description here

1

請試試這個代碼。 它在我身邊 xcode版本9.2

UIAlertController * alert= [UIAlertController 
           alertControllerWithTitle:nil 
           message:nil 
           preferredStyle:UIAlertControllerStyleActionSheet]; 
UIAlertAction* btn1 = [UIAlertAction 
         actionWithTitle:@"OKAY" 
         style:UIAlertActionStyleDefault 
         handler:^(UIAlertAction * action) 
         { 

         }]; 

[self presentViewController:alert animated:YES completion:nil];