菜鸟教程小白 发表于 2022-12-13 14:11:22

IOS Swift : Popup Custom view as alert box


                                            <p><p>**我用文字和图像设计了自己的 View 。</p>

<p>我想将此显示为警报 View 或仅显示为窗口</p>

<p>如何将 .modalPresentationStyle 用作 .Custom 并实现这一点</p>

<p>我应该使用 ViewController 还是直接显示 View </p>

<pre><code>@IBAction func BtnClickFnc(sender: AnyObject)
{
    let MsgBoxVar = MsgBoxCls()
    MsgBoxVar.modalPresentationStyle = .Custom
    MsgBoxVar.preferredContentSize   = CGSizeMake(200, 400)
    self.presentViewController(MsgBoxVar, animated: true, completion: nil)
</code></pre>

<p>}</p>

<pre><code>class MsgBoxCls: UIViewController
{
    override func viewDidLoad()
    {
      view.addSubview(NamVyuCls(frame: CGRectMake(50,50,200,200)))
    }
}


class NamVyuCls: UIView
{
    override public init(frame: CGRect)
    {
      super.init(frame: frame)

      layer.borderWidth = 2
      layer.borderColor = UIColor.darkGrayColor().CGColor
      layer.cornerRadius = 10

      // Code Todo
    }

    required init?(coder aDecoder: NSCoder) {
      fatalError(&#34;init(coder:) has not been implemented&#34;)
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><pre><code>var BgdVyuVar: UIView!
var NamVyuVar: NamVyuCls!

@IBAction func BtnKlkFnc(sender: AnyObject)
{
    BgdVyuVar = UIView(frame: view.frame)
    BgdVyuVar.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 225/255, alpha: 0.5)
    BgdVyuVar.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.DsmVyuFnc)))
    self.view.addSubview(BgdVyuVar)

    NamVyuVar = NamVyuCls()
    NamVyuVar.frame = CGRectMake(self.view.frame.width / 2 - 100,self.view.frame.height / 2 - 100,200,200)
    self.view.addSubview(NamVyuVar)

    UIView.animateWithDuration(0.5, delay: 0.1, options: .TransitionNone,
      animations:
      {
            self.view.bringSubviewToFront(self.NamVyuVar)
      },
      completion: nil)
}

func DsmVyuFnc()
{
    BgdVyuVar.removeFromSuperview()
    NamVyuVar.removeFromSuperview()
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于IOS Swift : Popup Custom view as alert box,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39763906/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39763906/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: IOS Swift : Popup Custom view as alert box