MBMenuController 2.0.3

MBMenuController 2.0.3

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2015年2月

Moshe Berman 维护。




  • 作者
  • Moshe Berman

描述

MBMenuController 类似于 UIActionSheet。这是我为一个项目所做的。

Promo

入门教程

使用 CocoaPods

`pod 'MBMenuController', '~>2.0.2'

不使用 CocoaPods

MBMenuController/Button Menu/ 抓取 MBMenuController.h/m ,并将它们添加到您的项目中。(代理协议在头文件中声明。)

依赖关系

您需要 iOS 7 SDK。我没有在更早版本的 iOS 上进行测试,但理论上,它应该可以与旧版本一起工作。

MBButtonMenuController 链接到 CoreGraphics.framework 和 UIKit.framework,但您不需要将它们添加到您的项目中。Xcode 会为您完成。

显示 MBBMenuController

/*

  After adding the two files to your Xcode project, 
  there are four steps to throwing up an 
  MBMenuController on the screen.

*/

// 0. Import MBMenuController.h
#import "MBMenuController.h"

// 1. Instantiate an instance with an array of titles
NSArray *titles = @[@"Red", @"Yellow", @"Green"];
MBMenuController *menu = [[MBMenuController alloc] initWithButtonTitles:titles];

// 2. Wire up the delegate, to handle tap events
[menu setDelegate:self];

// 3. Show it in a view
[menu showInView:[self view]];

处理按钮点击

在 MBMenuController.h 中定义了一个简单的委托 API。它包含两个方法,类似于 UIActionSheet。它们是

buttonMenuViewController:buttonTappedAtIndex:
buttonMenuViewControllerDidCancel:

当按钮被点击时,MBMenuController 首先检查取消按钮,然后(如果按钮不是取消按钮)触发 buttonMenuViewController:buttonTappedAtIndex:。在任何情况下,委托都有机会适当地处理事件。最简单的实现是关闭菜单。

关闭菜单

关闭菜单非常简单。只需使用这个单行命令

[menu hide];

可选 API

背景颜色:您可以通过调用 setBackgroundColor: 并传递一个 UIColor 来设置 MBMenuController 的颜色。此颜色也用于取消按钮文本。调用此命令不会导致菜单重新绘制自身,因此必须在设置按钮标题之前调用它。(问题 #4。)

取消按钮索引:您可以通过传递一个 NSUInteger 到 setCancelButtonIndex: 来告诉 MBMenuController 哪个按钮要作为取消按钮处理。如果索引超出了范围,则不会发生任何事情。传递一个介于零和按钮标题数组的边界之间的索引会导致设置索引并更新菜单。

如何工作

您使用按钮标题数组初始化一个MBMenuController实例,但您也可以稍后设置它们。调用setButtonTitles:会导致菜单再次渲染。当按钮太多而无法全部显示时,MBMenuController会使用滚动视图来处理。当按钮太多无法全部压缩时,菜单会滚动。为了处理点击事件,MBMenuController使用一个简单的委托,其中包含上述所述的两个方法。

当菜单显示时,它会导致“父”视图缩小。为了实现这一点,MBButtonMenuController会对要显示的视图应用一个变换,并将其安装在被目标视图覆盖的位置,以提供预期效果。

许可证

MBButtonMenuController现在根据MIT许可证发布。

版权所有 (c) 2013-2015 Moshe Berman

特此授予任何获得本软件及其相关文档副本(“软件”)的人免费使用该软件的权利,包括但不限于使用、复制、修改、合并、出版、分发、再许可和/或出售软件副本的权利,并授予任何人提供该软件的人以这样做,但受以下条件约束

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

该软件按“现有”提供,不做任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任承担责任,无论该责任是源于合同、侵权或其他方式,以及与软件的使用或其它任何联系。