JVMenuPopover

Simple popover menu in Objective-C

  • 所有者: JV17/JVMenuPopover
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

JVMenuPopover Version License Platform

This is a simple menu controller where I tried to simulate the native iOS animation of switching between apps. It can be used in many different ways and you can also customize it to use your own animations.

Previews

• Menu with slideInWithBounceAnimation

screenshot-1

• Menu with slideInAnimation

screenshot-1

Requirements

Developed and tested using iOS11+

Installation

JVMenuPopover is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "JVMenuPopover"

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

Using JVMenuPopover with your own project.

  • In your function application: didFinishLaunchingWithOptions: we need to setup our UIWindow, JVMenuNavigationController and your root view controller. Feel free to follow my project sample implementation where I use lazy intialization for these objects.

    • Also, it's important to set a background image to our UIWindow to be able to achieve the our menu effect with a background image.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // setting up app window
    [self setupCustomWindow];

    return YES;
}

#pragma mark - Custom Accessors

- (JVMenuRootViewController *)rootViewController
{
    if (!_rootViewController)
    {
        _rootViewController = [[JVMenuRootViewController alloc] init];
    }

    return _rootViewController;
}


- (JVMenuNavigationController *)navigationController
{
    if (!_navigationController)
    {
        _navigationController = [[JVMenuNavigationController alloc] initWithRootViewController:self.rootViewController transparentNavBar:YES];
    }

    return _navigationController;
}


#pragma mark - UIWindow Customization

- (void)setupCustomWindow
{
    self.window = [[UIWindow alloc] init];
    self.window.rootViewController = self.navigationController;
    self.window.backgroundColor = [UIColor colorWithPatternImage:[[UIImage imageNamed:@"app_bg1.jpg"] imageScaledToWidth:self.window.frame.size.width]];

    [self.window makeKeyAndVisible];
    [self.window addSubview:self.navigationController.view];
}
  • Then, in your rootController we need to set our data model for our menu using the class JVMenuItems which holds all the menu images, titles and the close button image to display. Also, we can set our preferred menu animation. And we need to create the JVMenuPopoverView which is the actual menu with the menu items. Note: The same approach here the use of lazy intialization.
- (JVMenuItems *)menuItems
{
    if(!_menuItems)
    {
        _menuItems = [[JVMenuItems alloc] initWithMenuImages:@[[UIImage imageNamed:@"home-48"],
                                                               [UIImage imageNamed:@"about-48"],
                                                               [UIImage imageNamed:@"settings-48"],
                                                               [UIImage imageNamed:@"business_contact-48"],
                                                               [UIImage imageNamed:@"ask_question-48"]]
                                                  menuTitles:@[@"Home",
                                                               @"About Us",
                                                               @"Our Service",
                                                               @"Contact Us",
                                                               @"Help?"]
                                         menuCloseButtonImage:[UIImage imageNamed:@"cancel_filled-50"]];
        _menuItems.menuSlideInAnimation = YES; 
    }

    return _menuItems;
}

- (JVMenuPopoverView *)menuPopover
{
    if(!_menuPopover)
    {
        _menuPopover = [[JVMenuPopoverView alloc] initWithFrame:self.view.frame menuItems:self.menuItems];
        _menuPopover.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        _menuPopover.delegate = self;
    }

    return _menuPopover;
}
  • Then, we need to set our menu bar button with our preferred image and set the target to show the menu.
- (void)viewDidLoad
{
    [super viewDidLoad];

    // creating menu
    self.menuPopover = [self menuPopover];

    // setting up menu bar button
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu_black-48"] style:UIBarButtonItemStylePlain target:self action:@selector(showMenu)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
}
  • Finally, we set our menu bar button to target to call the JVMenuPopoverView helper function and our menu delegate to know which menu item was selected by the user and present a new view controller or whatever you would like to then. Note: after iOS 9, we can't no longer present self on our rootController so, you might need to either have a property for the rootController and use that instead.
#pragma mark - Menu Helper Functions

- (void)showMenu
{
    [self.menuPopover showMenuWithController:self];
}


#pragma mark - Menu Delegate

- (void)menuPopoverDidSelectViewControllerAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        [self.navigationController setViewControllers:@[self.rootController]; // this is a temporary fix for the issue on iOS 9 not able to set 'self' as the current view controller
    }
    else if(indexPath.row == 1)
    {
        [self.navigationController setViewControllers:@[self.secondController]];
    }
}

Author & Support

Contact me if you find any bugs or potential room for improvements, which I am sure there are. Jorge Valbuena (@JV17), jorge.valbuena@jorgedeveloper.com. BTW! You are welcome to help in supporting this pod or making improvements to it.

License

JVMenuPopover is available under the MIT license. See the LICENSE file for more info.

Release Notes

Version 1.4

  • Improved landscape support. Now the menu closes prior rotation to avoid image size issues.
  • Added new delegates to Menu view to know when the menu view will show or hide.
  • Added new property to the Menu view which indicates if the view hidden or not.
  • Added optional functions on the rootController which hides & shows the navigationItem button (menu button), please sample project for this.

Version 1.3

  • Added landscape support. Please be advise that you might need to provide a bigger image allow the background to show properly.

主要指标

概览
名称与所有者JV17/JVMenuPopover
主编程语言Objective-C
编程语言Objective-C (语言数: 2)
平台
许可证MIT License
所有者活动
创建于2015-03-15 05:09:43
推送于2019-08-26 02:07:27
最后一次提交2019-08-25 22:07:22
发布数11
最新版本名称1.5 (发布于 )
第一版名称0.0.1 (发布于 )
用户参与
星数416
关注者数22
派生数74
提交数167
已启用问题?
问题数10
打开的问题数0
拉请求数0
打开的拉请求数0
关闭的拉请求数0
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?