自定义警报(UIAlertView)与swift [英] Custom Alert (UIAlertView) with swift

查看:132
本文介绍了自定义警报(UIAlertView)与swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Swift创建自定义提醒?我尝试翻译目标c中的指南但加载全屏布局



为了方便我可以加载带有透明背景的新布局我试试这个:

  listaalertviewcontroller.view.backgroundColor = UIColor.clearColor()
let purple = UIColor.purpleColor()// 1.0 alpha
让半= purple.colorWithAlphaComponent(0.5)

listaalertviewcontroller.view.backgroundColor =半


presentingViewController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext

self.presentViewController(listaalertviewcontroller,animated:true,completion:nil)

在动画中它是透明,但当动画结束时它是不透明的......我在视图中关闭了不透明的选项......我做错了什么?

解决方案

在Swift 4和Xcode 9中测试的代码



如何制作自己的自定义提醒



我想要做类似的事情。首先,不推荐使用 UIAlertView ,而选择 UIAlertController 。有关显示警报的标准方法,请参阅此答案:






  • 故事板



    您应该有两个视图控制器。您的第二个视图控制器将是您的警报。将类名设置为 AlertViewContoller ,将故事板ID设置为 alert 。 (这些都是我们在下面的代码中定义的名称,没有什么特别的。如果你愿意,可以先添加代码。如果你先添加代码,实际上可能会更容易。)





    将根视图的背景颜色(在警报视图控制器中)设置为明确。添加另一个 UIView 并以约束为中心。使用它作为你的警报背景,并把你想要的东西放在里面。在我的例子中,我添加了一个 UIButton





    代码



    ViewController.swift

     进口的UIKit 
    类的ViewController:UIViewController的{

    @IBAction FUNC showAlertButtonTapped(_发件人:的UIButton){

    设故事情节= UIStoryboard(名: 主,捆绑:无)
    让myAlert = storyboard.instantiateViewController(withIdentifier: 警报)
    myAlert.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
    myAlert.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
    self.present(myAlert,animated:true,completion:nil)
    }
    }

    AlertViewController.swift

      import UIKit 
    class AlertViewController:UIVie wController {

    @IBAction func dismissButtonTapped(_ sender:UIButton){
    self.dismiss(animated:true,completion:nil)
    }
    }

    不要忘记连接网点。



    就是这样。你应该能够做出你现在可以想象的任何警报。不需要第三方代码。



    其他选项



    有时候没有必要重新发明轮子,不过。我对第三方项目 SDCAlertView (MIT许可证)印象深刻。它是用Swift编写的,但您也可以将它与Objective-C项目一起使用。它提供了广泛的可定制性。


    How can i create a custom alert with Swift? I try translating a guide from Objective c but loads a full screen layout

    for do it easy i can load a new layout with the transparent background i try this:

        listaalertviewcontroller.view.backgroundColor = UIColor.clearColor()
        let purple = UIColor.purpleColor() // 1.0 alpha
        let semi = purple.colorWithAlphaComponent(0.5)
    
        listaalertviewcontroller.view.backgroundColor = semi
    
    
        presentingViewController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
    
        self.presentViewController(listaalertviewcontroller, animated: true, completion: nil)
    

    in the animation it's transparent but when the animation ends it's opaque... and i turn off opaque option in the view... what i'm doing wrong?

    解决方案

    Code tested in Swift 4 and Xcode 9

    How to make your own custom Alert

    I was wanting to do something similar. First of all, UIAlertView is deprecated in favor of UIAlertController. See this answer for the standard way to display an alert:

    And both UIAlertView and UIAlertController do not really allow much customization. One option is to use some third party code. However, I discovered that it isn't that difficult to create your own Alert by displaying another view controller modaly.

    The example here is just a proof-of-concept. You can design your alert any way you want.

    Storyboard

    You should have two View Controllers. Your second view controller will be your alert. Set the class name to AlertViewContoller and the Storyboard ID to alert. (Both of these are names that we defined ourselves in the code below, nothing special about them. You can add the code first if you want. It might actually be easier if you add the code first.)

    Set the background color for the root view (in your Alert View Controller) to clear. Add another UIView and center it with constraints. Use that as your alert background and put whatever you want inside. For my example, I added a UIButton.

    Code

    ViewController.swift

    import UIKit
    class ViewController: UIViewController {
    
        @IBAction func showAlertButtonTapped(_ sender: UIButton) {
    
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let myAlert = storyboard.instantiateViewController(withIdentifier: "alert")
            myAlert.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
            myAlert.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
            self.present(myAlert, animated: true, completion: nil)
        }
    }
    

    AlertViewController.swift

    import UIKit
    class AlertViewController: UIViewController {
    
        @IBAction func dismissButtonTapped(_ sender: UIButton) {
            self.dismiss(animated: true, completion: nil)
        }
    }
    

    Don't forget to hook up the outlets.

    That's it. You should be able to make any sort of alert that you can imagine now. No need for third party code.

    Other options

    Sometimes there is no need to reinvent the wheel, though. I'm impressed with the third party project SDCAlertView (MIT license). It is written in Swift but you can use it with Objective-C projects as well. It offers a wide range of customability.

    这篇关于自定义警报(UIAlertView)与swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆