iOS 如何向导航栏标题添加下拉菜单

How to add a dropdown menu to the Navigation Bar Title in iOS

我想制作类似于这些屏幕截图中的下拉菜单的东西。我该怎么做?

当您点击标题时,箭头指向上方,菜单会下拉。该应用程序是大学菜单。

有很多 cocoacontrols 可以在您的应用程序中执行此功能:

DropDown Menu Controls For iOS

不过我觉得lmdropdownview最准确

我的错误,btnavigationdropdownmenu它最准确地回答了你的问题(根据你的屏幕)。

实施(Swift)[有关更具体的说明和自定义,请参阅自述文件]:

let items = ["Most Popular", "Latest", "Trending", "Nearest", "Top Picks"]

let menuView = BTNavigationDropdownMenu(frame:  CGRectMake(0.0, 0.0, 300, 44), title: items.first!, items: items, containerView: self.view)

self.navigationItem.titleView = menuView

menuView.didSelectItemAtIndexHandler = {(indexPath: Int) -> () in
            println("Did select item at index: \(indexPath)")
            self.selectedCellLabel.text = items[indexPath]
}

希望对您有所帮助。

编辑:

感谢 'rsc' 的信息,这个 cocoacontrol 有一个 objective-c version

    #import "PFNavigationDropdownMenu.h"


-(void)viewDidLoad{
    PFNavigationDropdownMenu *menuView = [[PFNavigationDropdownMenu alloc]initWithFrame:CGRectMake(0, 0, 300, 44)title:items.firstObjects items:items containerView:self.view];

    menuView.didSelectItemAtIndexHandler = ^(NSUInteger indexPath){
            NSLog(@"Did select item at index: %ld", indexPath);
            self.selectedCellLabel.text = items[indexPath];
        };

        self.navigationItem.titleView = menuView;

    }

如果您喜欢手动操作,可能的解决方法:

创建两个 UIButton (up/down),将第一个分配给 navigationItem.titleView,在此操作上添加 UIControlEventTouchUpInside 的操作检查状态是否更改为展开或折叠并依赖于 add/remove UITableViewUIViewController view 上使用空数据源,设置适当的框架。然后更新数据源并使用行动画 UITableViewRowAnimationBottomUITableViewRowAnimationTop 重新加载,具体取决于它是展开还是折叠。将按钮替换为第二个按钮。