2015-03-03 88 views
7

我正在研究一个iPhone应用程序,该应用程序在单击栏按钮时出现的导航栏下有一个过滤器列表(下拉列表)。请告诉我该怎么做。swift中的下拉列表

enter image description here

+0

创建一个文本框或搜索栏,和一个tableview下面你的导航栏。完成搜索后,删除您的文本字段和tableview。 – 2015-03-03 10:18:15

回答

23

有许多方法可以做到这一点,我的建议将是类似如下的内容:

当初始化视图控制器,你的下拉菜单视图偏移和隐藏的导航栏后面。根据您的首选设置,使用布局约束或使用视图的框架执行此操作。

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 
} 

接着的动作连接起来的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() 
       } 
      }) 
    } 
} 

所有留给你的是定义你的dropDownView并正确地连接它。

我希望帮助,请与评论,如果您有什么不明白

+0

感谢您的重播,+1的速度答案。我会检查它然后我标记为正确的答案 – Hazem 2015-03-03 10:56:45

+0

伟大@Elliott(Y) – dip 2015-08-03 08:31:55

+0

我也实现了这种方式,但在我的我添加了两个UIButtons到下拉UIView。我在navigationController.navigationbar下面插入了下拉UIView。我的问题是我的点击事件不起作用。有任何想法吗 ? – DrPatience 2015-09-24 11:25:49

-1

要使用下拉列表中的自定义视图与泰伯维使用下面的链接https://github.com/lminhtm/LMDropdownView

+0

@abirama该库在Obj-C中,您有快速版本吗? – 2016-12-22 05:06:54

+0

在桥接头的帮助下,您可以在Swift Project中使用Obj-C。 – 2017-10-04 06:54:20