2016-09-29 66 views
2

**我设计了自己的视图,包含文本和图像。Ios Swift:Popup自定义视图作为提醒框

我想提出这样的警报视图或只显示为窗口

如何使用.modalPresentationStyle作为.Custom和达致这

应该使用视图控制器或只显示视图直接

@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") 
    } 
} 

回答

4
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() 
} 
+0

Ur的想法部分工作我希望它来到中心,灰色外面的区域和禁用所有其他东西就像弹出。当我在alertView外单击时,它也会退出。 –

+0

要灰化并禁用背景,请创建一个UIView。将它的背景色设置为blackColor,并将alpha属性设置为0.7并将其框架设置为主视图的框架。显示alertView时,将此UIView作为子视图添加到mainView。 要在外部单击时退出,请将UITapGestureRecognizer添加到新创建的backGround模糊视图。点击手势,删除警报视图 –

+0

是的,谢谢它的工作 –