简体   繁体   English

如何创建简单菜单?

[英]How to create simple menu?

I'm trying to create simple (facebook like) menu in my app. 我正在尝试在我的应用程序中创建简单的菜单(类似于facebook)。 I've found several questions, most of them had accepted answer, but usually answer is to use some programs done by developers. 我发现了几个问题,其中大多数都接受了答案,但通常的答案是使用开发人员开发的某些程序。

These programs are often in old version of xCode (one, that didn't use storyboards) and those, that were done in storyboard, were too complicated for me to implement in my app. 这些程序通常是旧版本的xCode(其中一个不使用情节提要),而那些在情节提要中完成的程序对于我来说太复杂了,无法在我的应用程序中实现。

So I found one question, which had as an answer something like this: 1. Create your menu view controller (UITableViewController for me) 2. Hide this controller in your initial view controller : 因此,我找到了一个问题,该问题的答案如下:1.创建菜单视图控制器(对我来说是UITableViewController)2.在初始视图控制器中隐藏该控制器:

MenuViewController *menView = [self.storyboard instantiateViewControllerWithIdentifier:@"Menu"];

    [self.view sendSubviewToBack:menView.view];

3 Create a button (maybe pan gesture later), in it's method, do something like this: 3创建一个按钮(以后可能会摇动手势),在该方法中,请执行以下操作:

-(IBAction)menuButtonPressed:(id)sender
 {
    CGRect destination = self.navigationController.view.frame;

    if (destination.origin.x > 0) {
        destination.origin.x = 0;
    } else {
        destination.origin.x +=254.5;
    }

    [UIView animateWithDuration:0.25 animations:^{
        self.navigationController.view.frame = destination;
    } completion:^(BOOL finished)
     {
         self.view.userInteractionEnabled = !(destination.origin.x > 0);
     }];


}

What this does I guess, it's that it basically moves your view to the right by fixed length. 我猜这是什么,基本上是将视图向右移动固定的长度。 I suppose, that your view, that you sendedToBack (your UITableView) should be exposed and interactable. 我想,您的视图,即sendedToBack(您的UITableView)应该公开并且可以交互。 However, all it does for me, is moving top view to the right, and leaving blank black-colored screen behind. 但是,对我来说,所有操作都是将顶视图向右移动,并在其后留下空白的黑色屏幕。

Now I think, that my problem is either bad instantiation of menuView, or just that I understanded this guide wrong. 现在我想,我的问题不是菜单视图的错误实例化,就是我误解了本指南。

If anyone knows, how to deal with this, I would be very thankful for an answer. 如果有人知道如何处理这个问题,我将非常感谢您的回答。 Maybe there is some already done app, that is as easy as this to understand and hopefully easier to implement in my code :) 也许有一些已经完成的应用程序,就这么容易理解,并希望在我的代码中更容易实现:)

Thanks! 谢谢!

I would do this using container views in storyboard. 我将使用情节提要中的容器视图执行此操作。 You can size the left one how you like, then add a right one next to it, and in the inspector change its width to 320 -- most of it will go off the screen to the right, and it will resize its embedded controller to be full screen size. 您可以按自己的喜好调整左一个的大小,然后在其旁边添加一个右一个,然后在检查器中将其宽度更改为320 -大多数将在屏幕右侧移出,并将其嵌入式控制器的大小调整为全屏尺寸。 You can delete the view controller that you get with the left container view, then drag out a table view controller, and connect it from the left view with an embed segue (it will be the only choice when you control drag from the container view). 您可以删除从左侧容器视图获得的视图控制器,然后将其拖出表视图控制器,并使用嵌入的segue从左侧视图连接它(当您从容器视图控制拖动时,它将是唯一的选择) 。 I added two swipe gesture recognizers (one left and one right) to the main controller's view and connected them to the 2 methods in the main controller. 我在主控制器的视图中添加了两个滑动手势识别器(一个向左,一个向右)并将它们连接到主控制器中的2个方法。 Then in the main controller I have this code in the .h: 然后在主控制器中,我在.h中有以下代码:

@interface ViewController : UIViewController

@property (weak,nonatomic) IBOutlet UIView *rightView;
@property (assign,nonatomic) CGRect rightRect;

@end

And in the .m, only this: 而在.m中,仅此:

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.rightRect = self.rightView.frame;
}


-(IBAction)revealSidebar:(id)sender {
    [UIView animateWithDuration:.3 animations:^{
        self.rightView.frame = self.view.window.frame;
    }];
}

-(IBAction)hideSidebar:(id)sender {
    [UIView animateWithDuration:.3 animations:^{
        self.rightView.frame = self.rightRect;
    }];
}
@end

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

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