簡體   English   中英

如何防止父級UIView從UIButton接收觸摸?

[英]How prevent a parent UIView to receive touch from UIButton?

我有一個UIView(從UIViewController)可以接收觸摸並刪除鍵盤。 在同一屏幕上,我有一個顯示密碼的UIButton,當我觸摸UIButton時,觸摸會轉到UIView並刪除鍵盤。

如何防止UIView從UIButton接收觸摸?

我會嘗試.isExclusiveTouch但是沒有用。

編輯

我的擴展名刪除了鍵盤:

import UIKit

public extension UIViewController {
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(
            target: self,
            action: #selector(UIViewController.dismissKeyboard)
        )
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }

    func dismissKeyboard() {
        view.endEditing(true)
    }
}

在我的ViewController中,按鈕:

self.myButton.addTarget(self, action: #selector(self.myButtonHandler), for: .touchUpInside)
self.view.addSubview(self.myButton)

不要使用手勢,只需使用此代碼

override func touchesBegan(_ touches: Set<UITouch>, with event:UIEvent?) {
    if let touch = touches.first {
        view.endEditing(true)
    }
    super.touchesBegan(touches, with: event)
}

在這里,當您觸摸視圖的任何部分(而非按鈕)時,鍵盤將被代碼關閉(view.endEditing(true))

嘗試將userIntrecationEnabled設置為false。

//example
yourView.isUserInteractionEnabled = false

如果禁用了視圖的用戶交互,它將忽略觸摸。

暫無
暫無

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

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