swift 中的下拉列表

Dropdown list in swift

我正在开发一个 iPhone 应用程序,它在导航栏下方有一个过滤器列表(下拉列表),当我单击导航栏按钮时会出现该列表。请建议我该怎么做。

有很多方法可以做到,我的建议类似于以下内容:

当您初始化视图控制器时,您的下拉视图会偏移并隐藏在导航栏后面。使用布局约束或使用视图的框架来执行此操作,具体取决于您的首选设置。

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
}

然后 link 对 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 并link正确设置它。

希望对你有帮助,有不明白的地方欢迎评论

要将下拉列表与自定义视图和 Tableview 一起使用,请使用以下 link https://github.com/lminhtm/LMDropdownView