ios 快速下拉列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28828882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dropdown list in swift
提问by Hazem
I am working on an iPhone app that has a filter list (dropdown list) under the navigation bar that appears when I click on the bar button. Please suggest me how can I do it.
我正在开发一个 iPhone 应用程序,该应用程序在单击栏按钮时出现的导航栏下方有一个过滤器列表(下拉列表)。请建议我该怎么做。
回答by Elliott Minns
There are a number ways to do it, my suggestions would be something similar to as follows:
有很多方法可以做到这一点,我的建议类似于以下内容:
When you initialise the view controller, your dropdown view is offset and hidden behind the navigation bar. Do this either with Layout Constraints or using the view's frame, depending on your preferred set up.
当您初始化视图控制器时,您的下拉视图会偏移并隐藏在导航栏后面。使用布局约束或使用视图的框架执行此操作,具体取决于您的首选设置。
var isAnimating: Bool = false
var dropDownViewIsDisplayed: Bool = false
func viewDidLoad() {
super.viewDidLoad()
let height: CGFloat = self.dropDownView.frame.size.height
let width: CGFloat = self.dropDownView.frame.size.width
self.dropDownView.frame = CGRectMake(0, -height, width, height)
self.dropDownViewIsDisplayed = false
}
Then link up an action to the BarButtonItem that, when pressed, displays the view if hidden or hides if visible using an animation.
然后将一个动作链接到 BarButtonItem,当按下时,显示视图(如果隐藏)或使用动画隐藏(如果可见)。
@IBAction func barButtonItemPressed(sender: UIBarButtonItem?) {
if (self.dropDownViewIsDisplayed) {
self.hideDropDownView()
} else {
self.showDropDownView()
}
}
func hideDropDownView() {
var frame: CGRect = self.dropDownView.frame
frame.origin.y = -frame.size.height
self.animateDropDownToFrame(frame) {
self.dropDownViewIsDisplayed = false
}
}
func showDropDownView() {
CGRect frame = self.dropDownView.frame
frame.origin.y = self.navigationBar.frame.size.height
self.animateDropDownToFrame(frame) {
self.dropDownViewIsDisplayed = true
}
}
func animateDropDownToFrame(frame: CGRect, completion:() -> Void) {
if (!self.animating) {
self.animating = true
UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveEaseInOut, animations: { () -> Void in
self.dropDownView.frame = frame
}, completion: (completed: Bool) -> Void in {
self.animating = false
if (completed) {
completion()
}
})
}
}
All that is left for you is to define your dropDownView and link it up correctly.
剩下的就是定义 dropDownView 并正确链接它。
I hope that helps, please comment if there is anything you don't understand
希望对你有帮助,有不明白的请留言
回答by Abirami Bala
To use Drop down List with custom View and with Tableview use the below link https://github.com/lminhtm/LMDropdownView
要在自定义视图和 Tableview 中使用下拉列表,请使用以下链接https://github.com/lminhtm/LMDropdownView