2017-04-21 26 views
0

創建自定義的編程文本框沒有拍了拍文本框

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

,並添加它像一個子視圖我認爲這是谷歌地圖的一個子視圖查看

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)我看到但它不可點擊,當我點擊它時什麼也沒有發生。我在這裏看到了一些這樣的問題,但沒有找到任何幫助我的東西。

+0

嘗試將其添加到mapView。大概觸摸事件隱藏在地圖視圖框 –

+0

我認爲你應該像這樣添加topBarView self.view.addSubview(topBarView) –

+0

@AhmedGinani非常感謝,它解決了我的問題 – sergs

回答

0

您需要使用xcode的視圖調試功能檢查UIView層次結構,並且需要檢查該文本框是否與其他視圖不重疊。

運行該應用程序。視圖調試可以在模擬器和設備上運行,但需要注意的是它必須是iOS 8模擬器或設備。也就是說,您可以允許您的項目中使用較早的部署目標,只要在嘗試查看調試時確保您在iOS 8上運行。 導航到您要在正在運行的應用程序中檢查的屏幕/視圖。

在導航器面板(左列)中,選擇Debug Navigator(第六個選項卡)。在你的過程旁邊,你會看到兩個按鈕 - 按最右邊的按鈕,並選擇查看用戶界面層次

+0

謝謝,現在我看到其他視圖與文本框重疊http://joxi.ru/Vm6bzpgsDKxOEm我可以通過某種方式解決此問題嗎? – sergs

+0

您的回答有助於我理解問題,@AhmedGinani幫助解決問題。謝謝) – sergs

0

我想這是因爲你把UITextField置於其他可觸摸的視圖下,所以觸摸事件被攔截。

如果您通過非defalut isUserInteractionEnabled對象創建自定義textField層次結構,請記住啓用它。