为什么我的程序生成的按钮没有附加到当前视图?

发布于 2024-09-15 08:46:39 字数 366 浏览 2 评论 0原文

下面的代码生成一个按钮并将其放置在屏幕上;

按钮 = [UIButton 允许] initWithFrame:CGRectMake(0.0, 0.0, 20.0, 20.0)]; 按钮.frame = CGRectMake(0.0, 20.0, 10.0, 40.0); 按钮.backgroundColor = [UIColor redColor]; [self.window addSubview:button];

但是,当我通过“pushViewController:animated”生成一个新屏幕以重叠当前屏幕时,按钮会浮动在新屏幕上。但是通过界面生成器附加到旧屏幕的所有其他内容都会以正确的方式消失在新屏幕后面。

那么,我做错了什么?

谢谢,

The code below generates a button and places it on the screen;

button = [UIButton allow] initWithFrame:CGRectMake(0.0, 0.0, 20.0, 20.0)];
button.frame = CGRectMake(0.0, 20.0, 10.0, 40.0);
button.backgroundColor = [UIColor redColor];
[self.window addSubview:button];

However, when I generate a new screen via "pushViewController:animated" to overlap the current screen, the buttons float about the new screen. But everything else attached to the old screen via Interface builder disappears the correct way behind the new screen.

So, what am I doing wrong?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

儭儭莪哋寶赑 2024-09-22 08:46:39

您想要将按钮添加到当前视图,而不是其下面的窗口。您应该在当前可见的 UIViewController 的 loadView 中创建按钮。尝试将其添加到该控制器的视图中,如下所示:

[self.view addSubview:button];

You want to add the button to the current view, not the window underlying it. You should be creating the button in loadView of the currently visible UIViewController. Try adding it to that controller's view like this:

[self.view addSubview:button];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文