簡體   English   中英

不要在iOS對話框區域之外用輕擊手勢關閉uialertcontroller工作表

[英]Don't dismiss uialertcontroller sheet with tap gesture outside dialog area in iOS

在我的iOS swift 3.0應用程序中,我在當前的ViewController上展示了UIAlertController表實例。 但是當我在表格的某個區域(變暗的半透明背景)之外點擊時,我不想解散該表格,因為我已經必須取消一個動作。 任何想法?

我有帶有更多按鈕的MGSwipeTableViewCell。 當用戶單擊該“更多”按鈕時,將執行以下代碼。

func onClickMore(for vmCell: VmCell) {
    let sheet = UIAlertController(title: vmCell.vmItem?.vmNameWithoutIp, message: vmCell.vmItem?.ipAddress, preferredStyle: .actionSheet)

    sheet.addAction(UIAlertAction(title: "Create Ticket", style: .default) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Start VM", style: .default) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Restart VM", style: .default) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Stop VM", style: .destructive) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Cancel", style: .cancel) { (action: UIAlertAction) in

    })

    present(sheet, animated: true) { 
        sheet.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))
    }
}

用於UIAlertController類型作為警報

您可以下載示例項目

將手勢識別器添加到alertController超級視圖中以處理用戶交互

self.present(alertController, animated: true, completion: {() -> Void in
 alertController.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil)
})

在那個動作上什么也不做

更新

let alertController = UIAlertController(title: "Do something", message: "With this", preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "Done", style: .default) { action in
        // perhaps use action.title here
    })

    self.present(alertController, animated: true, completion: {() -> Void in


        alertController.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))
    })

用於UIAlertController類型為actionSheet

您可以下載示例項目

你可以用兩種方法

選項1

 alert.view.superview.subviews[0] isUserInteractionEnabled = false

選項2

   alert.view.superview?.subviews[0].addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))

例如

   self.present(sheet, animated: true, completion: {() -> Void in
    //    sheet.view.superview?.subviews[0].isUserInteractionEnabled = false;
      sheet.view.superview?.subviews[0].addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))

    })

完整的代碼

  let sheet = UIAlertController(title: "karthik", message: "check with", preferredStyle: .actionSheet)

    sheet.addAction(UIAlertAction(title: "Create Ticket", style: .default) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Start VM", style: .default) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Restart VM", style: .default) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Stop VM", style: .destructive) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Cancel", style: .cancel) { (action: UIAlertAction) in

    })



    self.present(sheet, animated: true, completion: {() -> Void in
    //    sheet.view.superview?.subviews[0].isUserInteractionEnabled = false;
      sheet.view.superview?.subviews[0].addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))

    })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM