使用 swift 制作下拉列表?
Making a drop down list using swift?
swift中制作下拉菜单的库是什么?我是 Xcode 和 Swift 语言的新手,所以谁能指导我如何在 swift 中实现下拉列表?
A 'drop down menu' 是一个网络控制/术语。在 iOS 我们没有这些。您可能会更好地查看 UIPopoverController
。查看本教程,了解 PopoverControllers
不幸的是,如果您希望在 iOS9 中应用 UIPopoverController
,您将收到已弃用的 class 警告。相反,您需要设置所需视图的 UIModalPresentationPopover
属性 以获得相同的结果。
Popover
In a horizontally regular environment, a presentation style
where the content is displayed in a popover view. The background
content is dimmed and taps outside the popover cause the popover to be
dismissed. If you do not want taps to dismiss the popover, you can
assign one or more views to the passthroughViews property of the
associated UIPopoverPresentationController object, which you can get
from the popoverPresentationController property.
In a horizontally compact environment, this option behaves the same as
UIModalPresentationFullScreen.
Available in iOS 8.0 and later.
参考:https://developer.apple.com/documentation/uikit/uiviewcontroller/1621355-modalpresentationstyle
(Swift 3) 将文本框和 uipickerview 添加到故事板,然后将委托和数据源添加到 uipickerview,并将委托添加到文本框。关注视频寻求帮助
https://youtu.be/SfjZwgxlwcc
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
@IBOutlet weak var textBox: UITextField!
@IBOutlet weak var dropDown: UIPickerView!
var list = ["1", "2", "3"]
public func numberOfComponents(in pickerView: UIPickerView) -> Int{
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return list.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
self.view.endEditing(true)
return list[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.textBox.text = self.list[row]
self.dropDown.isHidden = true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == self.textBox {
self.dropDown.isHidden = false
//if you don't want the users to se the keyboard type:
textField.endEditing(true)
}
}
}
您必须确保使用 UIPickerViewDataSource 和 UIPickerViewDelegate 协议,否则它将在 swift 3
后引发 AppDelegate 错误
另请注意语法的变化:
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
现在是:
public func numberOfComponents(in pickerView: UIPickerView) -> Int
下面的内容对我有用。
import UIkit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var textBox: UITextField!
@IBOutlet weak var dropDown: UIPickerView!
var list = ["1", "2", "3"]
public func numberOfComponents(in pickerView: UIPickerView) -> Int{
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return list.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
self.view.endEditing(true)
return list[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.textBox.text = self.list[row]
self.dropDown.isHidden = true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == self.textBox {
self.dropDown.isHidden = false
//if you don't want the users to se the keyboard type:
textField.endEditing(true)
}
}
}
根据 Apple 的人机界面指南,使用 UIPickerview 是实现它的正确方法
如果您 select 在移动 safari 中下拉,它将显示 UIPickerview 让用户选择下拉项目。
或者
您可以使用 UIPopoverController 直到 iOS 9 已弃用,但最好坚持使用 UIModalPresentationPopover 您还想显示的视图
您可以使用 UIActionsheet 来显示项目,但最好使用 UIAlertViewController 并选择 UIActionSheetstyle 以显示前者在最新版本中已弃用
swift中制作下拉菜单的库是什么?我是 Xcode 和 Swift 语言的新手,所以谁能指导我如何在 swift 中实现下拉列表?
A 'drop down menu' 是一个网络控制/术语。在 iOS 我们没有这些。您可能会更好地查看 UIPopoverController
。查看本教程,了解 PopoverControllers
不幸的是,如果您希望在 iOS9 中应用 UIPopoverController
,您将收到已弃用的 class 警告。相反,您需要设置所需视图的 UIModalPresentationPopover
属性 以获得相同的结果。
Popover
In a horizontally regular environment, a presentation style where the content is displayed in a popover view. The background content is dimmed and taps outside the popover cause the popover to be dismissed. If you do not want taps to dismiss the popover, you can assign one or more views to the passthroughViews property of the associated UIPopoverPresentationController object, which you can get from the popoverPresentationController property.
In a horizontally compact environment, this option behaves the same as UIModalPresentationFullScreen.
Available in iOS 8.0 and later.
参考:https://developer.apple.com/documentation/uikit/uiviewcontroller/1621355-modalpresentationstyle
(Swift 3) 将文本框和 uipickerview 添加到故事板,然后将委托和数据源添加到 uipickerview,并将委托添加到文本框。关注视频寻求帮助 https://youtu.be/SfjZwgxlwcc
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
@IBOutlet weak var textBox: UITextField!
@IBOutlet weak var dropDown: UIPickerView!
var list = ["1", "2", "3"]
public func numberOfComponents(in pickerView: UIPickerView) -> Int{
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return list.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
self.view.endEditing(true)
return list[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.textBox.text = self.list[row]
self.dropDown.isHidden = true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == self.textBox {
self.dropDown.isHidden = false
//if you don't want the users to se the keyboard type:
textField.endEditing(true)
}
}
}
您必须确保使用 UIPickerViewDataSource 和 UIPickerViewDelegate 协议,否则它将在 swift 3
后引发 AppDelegate 错误另请注意语法的变化:
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
现在是:
public func numberOfComponents(in pickerView: UIPickerView) -> Int
下面的内容对我有用。
import UIkit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var textBox: UITextField!
@IBOutlet weak var dropDown: UIPickerView!
var list = ["1", "2", "3"]
public func numberOfComponents(in pickerView: UIPickerView) -> Int{
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return list.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
self.view.endEditing(true)
return list[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.textBox.text = self.list[row]
self.dropDown.isHidden = true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == self.textBox {
self.dropDown.isHidden = false
//if you don't want the users to se the keyboard type:
textField.endEditing(true)
}
}
}
根据 Apple 的人机界面指南,使用 UIPickerview 是实现它的正确方法
如果您 select 在移动 safari 中下拉,它将显示 UIPickerview 让用户选择下拉项目。
或者
您可以使用 UIPopoverController 直到 iOS 9 已弃用,但最好坚持使用 UIModalPresentationPopover 您还想显示的视图
您可以使用 UIActionsheet 来显示项目,但最好使用 UIAlertViewController 并选择 UIActionSheetstyle 以显示前者在最新版本中已弃用