【问题标题】:Dropdown list in swift快速下拉列表
【发布时间】:2015-03-03 10:01:47
【问题描述】:

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

【问题讨论】:

  • 在导航栏下方创建一个文本字段或搜索栏,以及一个表格视图。完成搜索后删除您的文本字段和表格视图。

标签: ios xcode swift animation


【解决方案1】:

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

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

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 并正确链接它。

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

【讨论】:

  • 感谢您的重播,+1 的快速回答。我会检查它然后我标记为正确答案
  • 我也以这种方式实现了它,但我在下拉 UIView 中添加了两个 UIButton。我在 navigationController.navigationbar 下方插入了下拉 UIView。我的问题是我的点击事件不起作用。有什么想法吗?
  • @Elliott,我在完成块中遇到错误。在“完成:(完成:布尔)-> 无效”行中。在表达式列表中说出预期的表达式
【解决方案2】:

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

【讨论】:

  • @abirama 那个库在 Obj-C 中,你有 swift 版本吗!?
  • 您可以借助桥接头在 Swift Project 中使用 Obj-C。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-04
  • 2018-12-13
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-11
相关资源
最近更新 更多