maz*_*mas 27 ios drop-down-menu swift
在swift中制作下拉菜单的库是什么?我是Xcode和Swift语言的新手,所以有人可以指导我如何在swift中实现下拉列表吗?
Cha*_*ier 28
(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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
"下拉菜单"是一个网络控件/术语.在iOS中我们没有这些.你可能会更好看UIPopoverController.查看本教程,了解PopoverControllers的一些见解
小智 8
不幸的是,如果你想UIPopoverController在iOS9中申请,你会得到一个弃用的警告.相反,您需要设置所需视图的 UIModalPresentationPopover属性以获得相同的结果.
酥料饼
在水平常规环境中,呈现样式,其中内容以弹出视图显示.背景内容变暗,弹出窗口外的水龙头导致弹出窗口被解除.如果您不想使用tap来关闭popover,可以将一个或多个视图分配给关联的UIPopoverPresentationController对象的passthroughViews属性,您可以从popoverPresentationController属性获取该属性.
在水平紧凑的环境中,此选项与UIModalPresentationFullScreen的行为相同.
适用于iOS 8.0及更高版本.
参考:https://developer.apple.com/documentation/uikit/uiviewcontroller/1621355-modalpresentationstyle
小智 5
您必须确保使用 UIPickerViewDataSource 和 UIPickerViewDelegate 协议,否则从 swift 3 开始它会抛出 AppDelegate 错误
另请注意语法的变化:
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
Run Code Online (Sandbox Code Playgroud)
就是现在:
public func numberOfComponents(in pickerView: UIPickerView) -> Int
Run Code Online (Sandbox Code Playgroud)
以下内容对我有用。
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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
90567 次 |
| 最近记录: |