2015-03-03 90 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