一个简单且可定制的水平菜单,当您不需要与 UICollectionView 碰碰运气时使用。
如果您使用 CocoaPods,只需简单地将以下行添加到您的 Podfile 中:
pod 'MPSHorizontalMenu'
使用 MPSHorizontalMenu 非常简单。您需要做的就是创建一个,将其添加到您的视图中,然后实现其数据源和代理的方法。从那里开始,所有行为都类似于您可能习惯于使用 UITableView 或 UICollectionView。
对于更多信息,请查看此存储库中包含的示例项目,并在遇到任何问题时报错。
// Declare this somewhere in your interface
@property (nonatomic, strong) MPSHorizontalMenu *horizontalMenu;
@property (nonatomic, strong) NSMutableArray *menuItems;
// Implement this somewhere in your View Controller or custom View
self.menuItems = [@[ @"Menu Item 1", @"Menu Item 2", @"Menu Item 3", @"Menu Item 4", @"Menu Item 5"] mutableCopy];
self.horizontalMenu = [[MPSHorizontalMenu alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, 41)];
self.horizontalMenu.itemSelectedBackgroundColor = [UIColor blueColor];
self.horizontalMenu.dataSource = self;
self.horizontalMenu.itemSelectedDelegate = self;
self.horizontalMenu.scrollsToTop = NO;
[self.horizontalMenu reloadData];
[self.horizontalMenu setSelectedIndex:0 animated:NO];
// Then implement the data source and delegate methods.
#pragma mark - MPSHorizontalMenuDataSource
- (int)numberOfItemsForMenu:(MPSHorizontalMenu *)horizontalMenu {
return [self.menuItems count];
}
- (NSString*)horizontalMenu:(MPSHorizontalMenu *)horizontalMenu titleForItemAtIndex:(NSUInteger)index {
return [self.menuItems objectAtIndex:index];
}
#pragma mark - MPSHorizontalMenuDelegate
-(void)horizontalMenu:(MPSHorizontalMenu *)horizontalMenu itemSelectedAtIndex:(NSUInteger)index {
if ([self.menuItems count] == 0) return;
// Do something when the index is changed.
}
MPSHorizontalMenu 可以通过多种方式进行自定义。您可以为正常和选中状态按钮的前景和背景颜色进行自定义
self.horizontalMenu.itemNormalBackgroundColor = [UIColor clearColor];
self.horizontalMenu.itemNormalForegroundColor = [UIColor whiteColor];
self.horizontalMenu.itemSelectedBackgroundColor = [UIColor blackColor];
self.horizontalMenu.itemSelectedForegroundColor = [UIColor greenColor];
菜单的背景颜色也是如此
self.horizontalMenu.backgroundColor = [UIColor redColor];
MPSHorizontalMenu 是 Mugunth Kumar 的 Mugunth Kumar 的 MKHorizMenu 的一个端口。特别感谢 Mugunth 对升级现有项目的祝福。
查看 LICENSE。