Sujay U N
Sujay U N

Reputation: 5340

Ios Swift : Popup Custom view as alert box

**I have designed my own view with text and images.

I want to present this as alert view or just display as window

How do I use .modalPresentationStyle as .Custom and acheive this

Should i use ViewController or just display view directly

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

}

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("init(coder:) has not been implemented")
    }
}

Upvotes: 2

Views: 1104

Answers (1)

Shayan Jalil
Shayan Jalil

Reputation: 588

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()
}

Upvotes: 4

Related Questions