2014-01-27 34 views
8

我有我有的alertview是和沒有選項。它看起來像下面。使用設置alertview是按鈕加粗和沒有按鈕到正常

enter image description here

代碼是

UIAlertView *confAl = [[UIAlertView alloc] initWithTitle:@"" message:@"Are you sure?" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil]; 
confAl.tag = 888; 
[confAl show]; 

這是完美的,但我想是的大膽和不正常的字體。

所以我切換了是和否按鈕,並有如下所示。使用

enter image description here

代碼是

UIAlertView *confAl = [[UIAlertView alloc] initWithTitle:@"" message:@"Are you sure?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; 
confAl.tag = 888; 
[confAl show]; 

有什麼辦法,我們可以有作爲是第一個按鈕,並沒有爲第二個按鈕是用加粗的效果呢?

注:我想在iOS 6中(同老款)&的iOS 7也同樣效果(新風格如上圖)。

+6

@downvoter:有什麼問題?我真的很喜歡恨你... –

+2

@Fahim ..寒意..我已經投票給你了。不恨那個選民.. :) –

+0

也可以嘗試一些這些:https://www.cocoacontrols.com/search?utf8=%E2%9C%93&q=alert+view – Woodstock

回答

4

您的要求是「突出顯示」是按鈕。默認情況下,iOS 7中的取消按鈕是突出顯示的按鈕。不幸的是,你不能簡單地在這裏改變alertViewStyle。但是你有一個解決方法。

看看this answer

+1

我會看到鏈接並回復給你... –

+0

同時你可以投票。 ..;)@FahimParkar –

+0

upvoted ...你可以看看我的問題[這裏](http://stackoverflow.com/questions/21364163/status-bar-becomes-gradient-after-closing-video-in -landscape-mode) –

13

您可以使用preferredAction默認屬性UIAlertController而不是UIAlertView

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"alert" message:@"My alert message" preferredStyle:UIAlertControllerStyleAlert]; 

UIAlertAction* yesButton = [UIAlertAction 
          actionWithTitle:@"OK" 
          style:UIAlertActionStyleDefault 
          handler:^(UIAlertAction * action) 
          { 

           [alertController dismissViewControllerAnimated:YES completion:nil]; 

          }]; 

UIAlertAction* noButton = [UIAlertAction 
          actionWithTitle:@"Cancel" 
          style:UIAlertActionStyleDefault 
          handler:^(UIAlertAction * action) 
          { 
           [alertController dismissViewControllerAnimated:YES completion:nil]; 
          }]; 

[alertController addAction:noButton];  
[alertController addAction:yesButton]; 
[alertController setPreferredAction:yesButton]; 

setPreferredAction將設置您的按鈕標題粗體。

+0

如果我們使用UIAlertController,這是答案 – FlySoFast

+0

由於UIAlertView [不建議](https://developer.apple.com/reference/uikit/uialertview),這是最好的答案 – Hannele

+0

我也是在我的代碼中使用'setPreferredAction'。但是這個*在iOS8中崩潰*,因爲它在iOS8中不可用。 你有什麼解決方案可以在iOS 8及以上版本上始終如一地工作嗎? –