【问题标题】:How to use the method "sendSubviewToBack" appropriately?如何正确使用“sendSubviewToBack”方法?
【发布时间】:2012-08-18 10:06:03
【问题描述】:

我已经实现了滑动菜单,但我想要与 facebook 完全一样的滑动效果。 我在stackoverflow上看到了以下帖子:

iphone facebook side menu using objective c

我想实施 greenisus 给出的解决方案(投票 15 次),但我在实施时遇到了麻烦,Bot 在 cmets 中对他的回答提出了同样的问题。 “@greenisus 我对你如何将菜单放在后面感到困惑?当我这样做时,它只会显示一个黑色的侧面菜单。” 这还没有回答。

他的答案供参考的文字是:

真的很简单。首先,您需要创建一个位于可见控制器下方的视图控制器。您可以像这样将该视图发送到后面:

[self.view sendSubviewToBack:menuViewController.view];

然后,您在导航栏的左侧放置一个菜单按钮,并编写如下处理程序:

- (void)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);
    }];
}

这是一般的想法。您可能需要更改代码以反映您的视图层次结构等。

只是想知道我们什么时候必须使用下面的方法,第二种方法简单而且效果很好,但是它下面不显示菜单视图。

[self.view sendSubviewToBack:menuViewController.view];

寻找一些指针或解决方案来正确运行上述代码。

【问题讨论】:

    标签: iphone objective-c ios uiview uiviewcontroller


    【解决方案1】:

    其实你应该知道我们该怎么做。 只需将 menuViewController.view 添加为 self.view 的子视图。但这将覆盖navigationController.view,所以你可以只[self.view sendSubviewToBack:menuViewController.view]。当您需要显示/隐藏 menuViewController 时,您需要使用方法 - (void)menuButtonPressed:(id)sender。

    在 HomeViewController 中:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // be careful with the sequence of the code. If you firstly add the contentViewController and then add the 
        // menuViewController you need to [self.view sendSubviewToBack:self.menuViewController.view], else you don't
        // need to use sendSubviewToBack.
        self.menuViewController = [[MenuViewController alloc] init];
        self.contentViewController = [[ContentViewController alloc] init];
        [self.view addSubview:self.menuViewController.view];
        [self.view addSubview:self.contentViewController.view];
    }
    

    在 ContentViewController 中:

    - (IBAction)showMenu:(id)sender
    {
        CGRect destination = self.view.frame;
        if (destination.origin.x > 0) {
            destination.origin.x = 0;
        } else {
            destination.origin.x += 254.5;
        }
        [UIView animateWithDuration:0.25 animations:^{
            self.view.frame = destination;        
        } completion:^(BOOL finished) {
            //self.view.userInteractionEnabled = !(destination.origin.x > 0);
        }];
    }
    

    【讨论】:

    • 得到它并正在寻找它,当我在github上探索一些库时,我发现那里也使用了相同的东西,感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2016-11-20
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多