没有在文本字段上点击

时间:2017-04-21 07:11:48

标签: ios swift xcode textfield

我以编程方式创建自定义文本字段

import UIKit

class SearchTextField: UITextField, UITextFieldDelegate {

let padding = UIEdgeInsets(top: 0, left: 40, bottom: 0, right: 5);

init(frame: CGRect, tintText: String, tintFont: UIFont, tintTextColor: UIColor) {
    super.init(frame:frame)
    self.frame = frame

    delegate = self
    backgroundColor = .white
    textColor = tintTextColor
    placeholder = tintText
    font = tintFont

    createBorder()

}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    delegate = self
}

func createBorder() {
    self.layer.cornerRadius = 6
    self.layer.borderColor = UIColor(red: 169/255, green: 169/255, blue: 169/255, alpha: 1).cgColor
    self.layer.borderWidth = 1
}

override func textRect(forBounds bounds: CGRect) -> CGRect {
    return UIEdgeInsetsInsetRect(bounds, padding)
}

override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
    return UIEdgeInsetsInsetRect(bounds, padding)
}

override func editingRect(forBounds bounds: CGRect) -> CGRect {
    return UIEdgeInsetsInsetRect(bounds, padding)
}
}

并将其作为子视图添加到我的视图中,这是Google地图视图的子视图

    import UIKit
import GoogleMaps

class MapViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var mapView: GMSMapView!

var customSearchBar: SearchTextField!
let searchBarTextColor = UIColor(red: 206, green: 206, blue: 206, alpha: 1)

override func viewDidLoad() {
    super.viewDidLoad()


    let camera = GMSCameraPosition.camera(withLatitude: 55.75, longitude: 37.62, zoom: 13.0)
    mapView.camera = camera
    mapView.isUserInteractionEnabled = true

    addTopBarView(mapView: mapView)
}



func addTopBarView(mapView: GMSMapView) {
    //heigt of topBar is 14% of height of view^ width is the same
    let topBarFrame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height * 0.14)
    let topBarView = UIView(frame: topBarFrame)
    addTopBarViewBackground(view: topBarView)
    addTitleForTopBarView(view: topBarView)
    addProfileIconForTopBarView(view: topBarView)
    addSettingsIconForTopBarView(view: topBarView)
    addSearchBar(view: topBarView)

    topBarView.isUserInteractionEnabled = true
    mapView.addSubview(topBarView)
}


func addSearchBar(view: UIView) {
    let frameCustomSearchBar = CGRect(x: 10, y: 45, width: view.frame.width - 20, height: 40)
    let fontCustomSearchBar = UIFont(name: "HelveticaNeueCyr", size: 28) ?? UIFont.italicSystemFont(ofSize: 14)
    let textColorCustomSearchBar = UIColor(red: 206/255, green: 206/255, blue: 206/255, alpha: 1)

    customSearchBar = SearchTextField(frame: frameCustomSearchBar, tintText: NSLocalizedString("find_petrole", comment: ""), tintFont: fontCustomSearchBar, tintTextColor: textColorCustomSearchBar)
    customSearchBar.delegate = self
    customSearchBar.isUserInteractionEnabled = true
    customSearchBar.isEnabled = true

    let iconPinView = UIImageView(image: #imageLiteral(resourceName: "icon_pin"))
    iconPinView.frame = CGRect(x: 10, y: 10, width: 12, height: 20)
    customSearchBar.addSubview(iconPinView)

    let iconAddView = UIImageView(image: #imageLiteral(resourceName: "icon_add"))
    iconAddView.frame = CGRect(x: customSearchBar.frame.width - 34, y: 10, width: 20, height: 20)
    customSearchBar.addSubview(iconAddView)

    view.addSubview(customSearchBar)
}

我看到的文本字段(customSearchBar)但它不可点击,当我点击它时没有任何反应。我在这里看到了一些这样的问题,但没有找到任何帮助我的东西。

2 个答案:

答案 0 :(得分:0)

您需要使用xcode的View Debugging功能检查UIView Hierarchy,并且需要检查textfield是否与其他视图不重叠。

运行该应用。 View Debugging可在模拟器和设备上运行,但重要的是要注意它需要是iOS 8模拟器或设备。也就是说,您可以在项目中允许更早的部署目标,只需确保在尝试View Debugging时在iOS 8上运行。 导航到要在正在运行的应用程序中检查的屏幕/视图。

在“导航器”面板(左栏)中,选择“调试导航器”(第六个选项卡)。在您的流程旁边,您会看到两个按钮 - 按最右边的按钮并选择查看UI层次结构

答案 1 :(得分:0)

I guess it's because you put the UITextField under other touchable views so the touch event was intercepted.

if you make the custom textField hierarchy by a non-defalut isUserInteractionEnabled object, remember to enable it.