2017年5月7日 星期日

Swift3 AlertController 的一些基本運用

今天要來寫AlertController的一些運用,使用都是原生的Code,在iOS9以後apple要求使用AlertController,在這之前大家都是使用已經停的AlertView。如果你遇到要使用的情況建議使用AlertController唷!畢竟官方已經停用AlertView
有一個小小的提醒就是當您在練習時記得不要將AlertController寫在viewdidLoad因為可能會沒顯示出來,建議使用一個button或是在比viewdidLoad晚執行的時候處理

一般的AlertController建立

 let alertController = UIAlertController(title: "建立Alert", message: "建立成功了唷!", preferredStyle: .alert);
        //建立AlertAction,如果再按下Action後要做動作就將print的位子寫入你要的動作,像取消通常沒做動作可將handler帶入nil即可
        let alertActionOK = UIAlertAction(title: "確定", style: .default, handler: {
            action in
            print("寫入你要的動作");
        });
        let alertActionCancel = UIAlertAction(title: "取消", style: .destructive, handler: nil);//style通常都是使用default,如使用.destructiveaction會直些顯示紅色
        
        alertController.addAction(alertActionOK);//AlertController加入確定鈕
        alertController.addAction(alertActionCancel);//AlertController加入取消鈕
        //顯示出AlertController的方法有下面兩種。
        self.present(alertController, animated: true, completion: nil);

//        self.show(alertController, sender: nil);


改變AlertController的背景色

 let alertController = UIAlertController(title: "背景色?", message: "改變背景色", preferredStyle: .alert);
        //加入背景色
        let firstView = alertController.view.subviews.first;
        let backView = firstView?.subviews.first;
        for subview in (backView?.subviews)!{
            subview.backgroundColor = UIColor.yellow;
            subview.layer.cornerRadius = 10.0;
            subview.alpha = 1;
        }
        
        let alertActionOK = UIAlertAction(title: "確定", style: .default, handler: nil);
        alertController.addAction(alertActionOK);
        self.present(alertController, animated: true, completion: nil);



改變AlertAction的文字顏色

let alertController = UIAlertController(title: "Action顏色", message: "改變Action顏色", preferredStyle: .alert);
      //這行是兩個Action顏色都改一樣
//        alertController.view.tintColor = UIColor.brown
        
        let alertActionOK = UIAlertAction(title: "確定", style: .default, handler: nil);
        //改變ActionOK的顏色為橘色
        alertActionOK.setValue(UIColor.orange, forKey: "titleTextColor");
        
        let alertActionCancel = UIAlertAction(title: "取消", style: .default, handler: nil);
//        改變ActionCancel的顏色為綠色
        alertActionCancel.setValue(UIColor.green, forKey: "titleTextColor");
        
        alertController.addAction(alertActionOK);
        alertController.addAction(alertActionCancel);
        self.present(alertController, animated: true, completion: nil);



改變AlertController的Title文字顏色

 let alertController = UIAlertController(title: "Title顏色改變", message: "預設顏色為黑色唷!改為紅色了", preferredStyle: .alert);
        let myTitleString = NSMutableAttributedString(string: "Title顏色改變",
                                                      attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!]);
        myTitleString.addAttribute(NSForegroundColorAttributeName,
                                   value: UIColor.red,
                                   range: NSRange(location: 0,
                                                  length: "Title顏色改變".characters.count));
        
        alertController.setValue(myTitleString, forKey: "attributedTitle");
        
        let alertActionOK = UIAlertAction(title: "確定", style: .default, handler: nil);
        alertController.addAction(alertActionOK);
        self.present(alertController, animated: true, completion: nil);




改變AlertController的Message文字顏色

 let message = "改變訊息的顏色變為橘色了喔!"
         let alertController = UIAlertController(title: "Message顏色改變", message: message, preferredStyle: .alert);
        let messageMutableString = NSMutableAttributedString(string: message, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 15.0)!]);
        messageMutableString.addAttribute(NSForegroundColorAttributeName,
                                          value: UIColor.orange,
                                          range: NSRange(location: 0, length: message.characters.count));
        
        alertController.setValue(messageMutableString, forKey: "attributedMessage");
        
        let alertActionOK = UIAlertAction(title: "確定", style: .default, handler: nil);
        alertController.addAction(alertActionOK);
        self.present(alertController, animated: true, completion: nil);



AlertController中加入TextField

 let alertController = UIAlertController(title: "加入textField?", message: "下方有TextField", preferredStyle: .alert);
        alertController.addTextField(configurationHandler: {(textField : UITextField!) -> Void in
            textField.placeholder = "請輸入文字"
            //怎麼運用看個人了喔!如果想要鎖定字數之類的請慘考此篇http://h81061678.blogspot.tw/2016/12/swift3-alertaction.html
        });
        let alertActionOK = UIAlertAction(title: "確定", style: .default, handler: nil);
        alertController.addAction(alertActionOK);

        self.present(alertController, animated: true, completion: nil);



以上的用法希望能夠對大家有幫助唷!