我希望用户只有在他/她在swift 3中点击uipickerview时才选择国家/地区

时间:2017-11-24 06:22:08

标签: ios swift uigesturerecognizer uipickerview

我在UIPickerView中创建了国家/地区列表,并在UItextField中调用它。现在,我希望用户点击UIPIckerView来选择国家名称。我做了一些关于堆栈溢出的研究,发现(令人惊讶的是!!)这样做并不容易。我为此目的编写了以下代码,它向我显示以下错误:

 Cannot convert value of type 'CGRect' to expected argument type 'CGFloat'

有一种简单的方法吗?它是非常基本的要求应该非常简单;否则,有人可以帮助解决我在创建tapped函数时遇到的错误

下面是我的代码

 class StudentSignUpViewController: UIViewController,UIScrollViewDelegate, UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource, UIGestureRecognizerDelegate {




@IBOutlet weak var countryText: UITextField!
@IBOutlet weak var scrollView: UIScrollView!

///who are you option dropdown box
var picker = UIPickerView()

override func viewDidLoad() {
    super.viewDidLoad()

    //setting portrait
    AppUtility.lockOrientation(.portrait)

    //hide keyboard when click outside
    self.hideKeyboardWhenTappedAround()

    //hide keyboard when click on return

    self.countryText.delegate = self

    self.scrollView.delegate = self

    picker.delegate = self
    picker.dataSource = self
    self.countryText.inputView = picker

    let tap = UITapGestureRecognizer(target: self, action: #selector(pickerTapped))
    tap.cancelsTouchesInView = false
    tap.delegate = self
    picker.addGestureRecognizer(tap)

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    AppUtility.lockOrientation(.portrait)

}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    AppUtility.lockOrientation(.all)
}


//*************************************************************** creating dropdown for country list ***************************************************************

let countries = NSLocale.isoCountryCodes.map { (code:String) -> String in
    let id = NSLocale.localeIdentifier(fromComponents: [NSLocale.Key.countryCode.rawValue: code])
    return NSLocale(localeIdentifier: "en_US").displayName(forKey: NSLocale.Key.identifier, value: id) ?? "Country not found for code: \(code)"
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return self.countries.count
}


func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
    return self.countries[row];
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
    self.countryText.text = self.countries[row];
    self.countryText.endEditing(true)
}


func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true}

@objc func pickerTapped(tapRecognizer:UITapGestureRecognizer)
{
    if (tapRecognizer.state == UIGestureRecognizerState.ended)
    {
        let frame = CGRect.zero
        let rowHeight : CGFloat  = self.picker.rowSize(forComponent: 0).height
        let selectedRowFrame: CGRect = frame.insetBy(dx: self.picker.bounds, dy: (self.picker.frame.height - rowHeight) / 2.0 ) //Error: Cannot convert value of type 'CGRect' to expected argument type 'CGFloat'
        let userTappedOnSelectedRow = (selectedRowFrame.contains(tapRecognizer.location(in: picker)))
        if (userTappedOnSelectedRow)
        {
           self.picker.selectedRow(inComponent: 0)

        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

根据苹果标准,您应该将toolbar添加到具有按钮done的pickerview,这将取消选择器视图。通过滚动选取器视图行并将所选行保持在中心段,已经进行了选择。但如果您的要求非常严格,那么您应该创建自己的自定义组件