gpt4 book ai didi

objective-c - SideMenu 黑色 View

转载 作者:行者123 更新时间:2023-11-29 11:07:05 27 4
gpt4 key购买 nike

我正在尝试制作一个侧边栏菜单,但我遇到了一个小问题。

我解释:

  1. 我创建了一个名为 sideMenuViewController 的 UIViewController
  2. 在我的 viewController 类(初始 View Controller )中,在头文件中,我导入了我的类 SideMenuViewController 并且我写道:

    -(IBAction)openSideMenu:(id)发件人;

    @property(nonatomic, retain) SideMenuViewController *sideMenu;

openSideMenu 操作与菜单按钮关联。 Menu button

我是这样实现这个方法的:

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

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

[UIView animateWithDuration:0.4 animations:^{
self.view.frame = destination;
}completion:^(BOOL finished) {
if(finished){

}
}];
}

SideMenuX 是一个宏:#define SideMenuX 154.4

我的 viewDidLoad 方法如下所示:

- (void)viewDidLoad
{
[super viewDidLoad];
_sideMenu = [[SideMenuViewController alloc] init];
[self.view sendSubviewToBack:_sideMenu.view];
// Do any additional setup after loading the view, typically from a nib.
}

问题是,当我点击菜单按钮时,出现黑屏,而不是侧面菜单 View 。

Black screen side Bar Menu

提前致谢!

最佳答案

两个问题:

  1. 您根本没有添加侧边菜单。尝试将它添加到父 View (self.view.superview),在您的情况下很可能是 UIWindow:
     [self.view.superview insertSubview:_sideMenu.view belowSubview:self.view];
    If you are using a navigation controller, use self.navigationController.view instead self.view.
  2. Not sure if you initialized the view with a NIB or the Storyboard (see below if you didn't).

Here is a working example. I created the left view controller inside the storyboard like this:

  • Throw a View Controller component on the storyboard.
  • Select the controller on the left column, and go to the Identity Inspector on the right column (alt+cmd+3):
    • Set the Class to SideMenuViewController
    • Set the Storyboard ID to SideMenuViewController

Instantiate the controller inside viewDidLoad with

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
self.sideMenu = (SideMenuViewController*)[storyboard instantiateViewControllerWithIdentifier:@"SideMenuViewController"];

然后将其作为父 View 的子项插入。


(回答下面的评论)

这一行是问题所在:

[self.view.superview addSubview:_sideMenu.view];

在基于 NIB 的项目中,superview 是 UIWindow,但在 Storyboard 项目中,UIViewController 的 self.view.superview 是 nil。你可以解决这个问题,例如,添加一个 UINavigationViewController。请按照以下步骤操作:

  • 加入“导航 Controller ”
  • 删除它指向的 View Controller 。
  • 按住 Ctrl 并将指针从 UINavigationController 拖动到您的 View Controller ,然后在出现的对话框中选择“ Root View Controller ”。
  • 将指向您的 View Controller 的箭头拖动到 UINavigationController(标记初始 View Controller 的那个,而不是来自 UINavigationController 的那个)。

然后将您的代码更改为

_sideMenu = [[SideMenuViewController alloc] initWithNibName:@"SideMenuViewController" bundle:nil];
[self.navigationController.view.superview insertSubview:_sideMenu.view belowSubview:self.navigationController.view];

要隐藏 UINavigationController 的导航栏,请在 Storyboard 中选择它并在 Attributes Inspector 中单击 Hidden (alt+cmd+4)。

关于objective-c - SideMenu 黑色 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13136055/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com