2016-11-18 17 views
4

我試圖創建一個自定義UIAlertController風格UIAlertControllerStyleActionSheet(原UIActionSheet),我遇到了這麼大的麻煩簡單地定製。我想讓我的UIAlertAction爲包含左側圖像減小按鈕大小UIAlertController和UIAlertControllerStyleActionSheet定製

我知道這是feasable:

enter image description here

(這正是我在尋找)

我搜索了幾個小時,但我無法找到任何嘖嘖的網絡,可以幫助上我要完成這個簡單的任務。此外,由於UIActionSheet自IOS 9以來已棄用,因此無論何時我嘗試找到解決方案,它都不再可用。另外,我讀了UIAlertController不打算分類...

有誰知道如何實現我的警報輕定製? 我是否需要子類化一個UIView來製作我自己的報警表?

回答

3

此解決方案確實使用私有API /屬性。根據我的研究和經驗,這是我知道您可以自定義UIAlertController的唯一方法。如果您查看公共標題,則UIAlertContoller幾乎沒有自定義空間。然而,在iOS 8中啓動UIAlertController之後,此解決方案通常用於開發人員。您完全可以依賴Github中的依賴關係。我希望我的回答能解決你的問題。 我相信這是你在找什麼,結果loooks這樣的:

首先,你必須創建一個UIAlertController

UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Alert Title" message:@"" preferredStyle:UIAlertControllerStyleActionSheet]; 

自定義字體!即使只是某些角色。

NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... StackOverFlow!"]; 
[hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30.0] range:NSMakeRange(24, 11)]; 
[alertVC setValue:hogan forKey:@"attributedTitle"]; 

現在,讓我們創建一個UIAlertAction,在這裏你可以添加動作處理程序,並添加圖標。

UIAlertAction *button = [UIAlertAction actionWithTitle:@"First Button" 
               style:UIAlertActionStyleDefault 
               handler:^(UIAlertAction *action){ 
                //add code to make something happen once tapped 
               }]; 
UIAlertAction *button2 = [UIAlertAction actionWithTitle:@"Second Button" 
               style:UIAlertActionStyleDefault 
               handler:^(UIAlertAction *action){ 
                //add code to make something happen once tapped 
               }]; 

在這裏,您將圖標添加到AlertAction。對於我來說,你必須指定UIImageRenderingModeAlwaysOriginal

[button setValue:[[UIImage imageNamed:@"image.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"]; 

[alertVC addAction:button2]; 
[alertVC addAction:button]; 

記住要呈現的ViewController

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

enter image description here

+0

此方法不允許我調整按鈕高度。 UIAlert佔據了整個屏幕,這是非常侵入性的! – Olympiloutre

+0

您的按鈕高度由您的圖標和字體的大小決定。否則它是默認大小。 – NeilNie

+0

此解決方案利用未公開的私人功能,這些功能可能會在iOS的未來更新中崩潰(崩潰或無法正常工作)。最好避免這樣的代碼。 「UIAlertController」或「UIActionSheet」都不支持通過公共API提供的此功能。因此你應該找到一個定製的解決方案 – rmaddy

0

對於那些誰可能會感興趣我怎麼DIT來克服這個問題,我發表我的解決辦法:

我終於放棄了UIAlertController,這是n不打算分類。取而代之的是,在我的ViewController,我創建了一個程序重新創建相同的效果和渲染的方法:

  1. 創建的UIView佔據整個屏幕的設置它的阿爾法〜0.7。(這有兩個目的,一個美學一個和物理一個隱藏當前視圖,避免後面的定製警報按鈕被點擊)

  2. 創建UIButtons並手動將其放置在當前視圖。

  3. 將此前景視圖和按鈕添加到當前視圖,並且單擊按鈕時,只需將其刪除即可。

示例代碼:

-(void) displayAlertChoice{ 

    // create foreground layer to hide current view 
    foregroundLayer = [[UIView alloc] initWithFrame:CGRectMake(0,0,screenWidth,screenHeight)]; 
    foregroundLayer.backgroundColor = [UIColor blackColor]; 
    foregroundLayer.alpha = 0.0; 
    [self.view addSubview:foregroundLayer]; 
    [UIView animateWithDuration:0.2 animations:^{foregroundLayer.alpha = 0.7;}]; // animation de son apparition (alpha) 

    // Hide foreground layer when clicked 
    UITapGestureRecognizer *tapForegroundLayer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickForegroundLayer:)]; 
    [foregroundLayer addGestureRecognizer:tapForegroundLayer]; 


    // CancelButton 
    cancelButton= [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [cancelButton setFrame:CGRectMake(marginWidth, 
             2*screenHeight - buttonHeight - marginHeight, // OFF SCREEN 
             buttonWidth, 
             buttonHeight)]; 
    [cancelButton setTitle:@"Annuler" forState:UIControlStateNormal]; 
    [cancelButton addTarget:self action:@selector(alertCancelButton:) forControlEvents:UIControlEventTouchUpInside]; 
    [cancelButton setBackgroundColor:[UIColor whiteColor]]; 
    cancelButton.layer.cornerRadius = 8; 
    [self.view addSubview:cancelButton]; 
    [UIView animateWithDuration:0.2 animations:^{ // animation de son apparition (fram up) 
     [cancelButton setFrame:CGRectMake(marginWidth, 
              screenHeight - buttonHeight - marginHeight, // ON SCREEN 
              buttonWidth, 
              buttonHeight)]; 
    }]; 
} 

此代碼不僅僅是加入視圖和按鈕,以重新創建動畫時警報showes起來稍微複雜一些:

  • 使用[UIView animateWithDuration:動畫:^ {}];我能夠逐漸遮掩前景視圖並從屏幕底部顯示取消按鈕。
  • 注:可以不加UIButtons直接向foregroundView(這裏稱爲foregroundLayer),否則會輻照誘導的0.7阿爾法我們設置的早期
  • 我加入了手勢識別選擇到具有相同效果的foregroundView cancelButton選擇器:它從超級視圖中刪除所有創建的對象。這樣,當用戶在列表邊緣外單擊時,它會關閉自定義AlertView。