2016-09-29 54 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

是的,謝謝它的工作 –