简体   繁体   English

如何在iPhone中创建物理菜单按钮(如Android中的菜单按钮)?

[英]How to create physical menu button (like the one in Android) menu in iPhone?

I am developing a navigation based application in iPhone. 我正在开发iPhone中基于导航的应用程序。

In which I want to create physical menu button like menu like the one in Android phones. 我想在其中创建类似于Android手机中的菜单之类的物理菜单按钮。

The menu can be accessed from any view in the hierarchy. 可以从层次结构中的任何视图访问菜单。

Do any one have any idea how can we achieve this . 任何人都不知道我们如何实现这一目标。

Can we add a drawer like menu to UINavigationBar ? 我们可以在UINavigationBar中添加类似菜单的抽屉吗?

Please don't suggest the tabbarcontroller as it is the last option. 请不要建议tabbarcontroller,因为它是最后一个选项。

I think that what you want is just set the titleView of UINavigationItem to an arbitrary UIView to handle user taps. 我认为您只需要将UINavigationItem的titleView设置为任意UIView即可处理用户点击。

This doesn't have to be a text label :) 这不必是文本标签:)

If that's the case then I'd suggest you taking sharedInstance with flags tracking the hierarchy of the view controller stack. 如果是这种情况,那么我建议您采用带有跟踪视图控制器堆栈层次结构的标志的sharedInstance Every time when user navigate just update your sharedInstance and do the custom menu preparation and populate it. 每次用户导航时,只需更新您的sharedInstance并进行自定义菜单准备并填充它。 The good practice would be reuse your UI component rather than making it new on every navigation operation. 好的做法是重用您的UI组件,而不是在每次导航操作中都对其进行更新。

What you should do is create a subclass of UIViewController , eg MasterViewController . 您应该做的是创建UIViewController的子类,例如MasterViewController Have all of the view controllers subclass MasterViewController . 具有所有视图控制器子类MasterViewController In the MasterViewController you can write code to create and display your drawer view, and then all of your view controllers will inherit that code and can call it on themselves at any point. MasterViewController您可以编写代码来创建和显示抽屉视图,然后所有视图控制器都将继承该代码并可以随时对其进行调用。

To slide the view in: 滑动视图:

drawerView.frame = CGRectMake(0, -1*drawerView.frame.size.height, self.view.frame.size.width, whateverHeight);
[UIView beginAnimations:nil context:NULL];
drawerView.frame = CGRectMake(0, 0, self.view.frame.size.width, whateverHeight);
[UIView commitAnimations];

You can create a " DrawerView " class and import it into where you will want to use it. 您可以创建一个“ DrawerView ”类,并将其导入到要使用它的位置。 Then you can use presentModalViewController: myDrawer to make it appear when you need it. 然后,您可以使用presentModalViewController: myDrawer使它在需要时显示。 Hope that makes sense. 希望有道理。

Its very simple.. 非常简单

In one XIB u can add many views right. 在一个XIB中,您可以添加许多视图。 now add two main views like say data view and down side button view u want in. (if m nt wronge with 4 buttons to navigate) now keep ur this view at top . 现在添加两个主要视图,例如说数据视图和您想要的向下按钮视图。(如果有4个按钮需要导航)现在将您的视图保持在顶部。 and all the other hirarchy of views u can add in that data view. 您可以在该数据视图中添加所有其他视图层次结构。

I think you should create a singleton UIViewController or UIView for this. 我认为您应该为此创建一个单例UIViewControllerUIView Advantage of the UIViewController is that it can be presented with the presentModalViewController:animated: method, and also if you need, you can push it to your navigation stack. UIViewController优点是可以使用presentModalViewController:animated:方法来呈现它,并且如果需要,也可以将其推送到导航堆栈中。 Also an advantage that you have a UIView instance in the same time. 同时拥有一个UIView实例的另一个优点是。
Using the singleton pattern, you can reach your class anywhere. 使用单例模式,您可以在任何地方上课。

Check this link from the official documentation about singleton in objective-c: Creating a Singleton Instance 检查官方文档中有关Objective-c中的单例的链接: 创建单例实例
Or you can use Matt Gallagher's macro (because this is very comfortable to use): Singleton macro 或者您可以使用Matt Gallagher的宏(因为使用起来非常舒适): Singleton宏

If you can get iOS 5 beta, I highly recommend it. 如果您可以获得iOS 5 Beta,我强烈推荐您。 The new beta dev release has an extension on UIViewController , dubbed by Apple as container view controllers. 新的beta开发版本在UIViewController上进行了扩展,被Apple称为容器视图控制器。 They are very easy to set up, and I am using one in my app. 它们非常容易设置,我正在我的应用程序中使用一个。

Container view controllers work like UINavigationControllers and UITabBarControllers in that they allow you to add child view controllers , not just child views. 容器视图控制器的工作方式类似于UINavigationControllersUITabBarControllers ,它们使您可以添加子视图控制器 ,而不仅仅是子视图。 Like I've said, they are simple to set up. 就像我说的那样,它们很容易设置。 I'll explain how: 我将解释如何:

  1. Create a new class called ContainerController . 创建一个名为ContainerController的新类。 Add an initialization method called - (id)initWithNavigationController:(UINavigationController *)controller . 添加一个名为- (id)initWithNavigationController:(UINavigationController *)controller的初始化方法。 It's implementation should look something like this: 它的实现应如下所示:

     if ((self = [super init])) { [self addChildViewController:controller]; controller.view.frame = CGRectMake(0, 0, 320, 420); // Leave room for the menu at bottom of the screen [self.view addSubview:controller.view]; } 

    The reason we are adding a navigation controller is because it keeps this container controller from doing transitions, which is more complicated. 我们添加导航控制器的原因是因为它使此容器控制器无法进行转换,这更加复杂。

  2. In the ContainerController 's viewDidLoad method, create the menu: ContainerControllerviewDidLoad方法中,创建菜单:

     [super viewDidLoad]; UIView *menuWrapperView = [[[UIView alloc] init] autorelease]; menuWrapperView.frame = CGRectMake(0, 420, 320, 40); // Place a wrapper for the menu buttons directly below the navigation controller UIButton *button1 = ...; UIButton *button2 = ...; [menuWrapperView addSubview:button1]; [menuWrapperView addSubview:button2]; [self.view addSubview:menuWrapperView]; 

With this approach (using child view controllers), your views and navigation controller will get their needed memory warning, view loading, and rotation handling method calls when needed. 通过这种方法(使用子视图控制器),视图和导航控制器将在需要时收到其所需的内存警告,视图加载和旋转处理方法调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM