gpt4 book ai didi

objective-c - 如何从导航栏中删除按钮?

转载 作者:行者123 更新时间:2023-11-28 19:22:04 25 4
gpt4 key购买 nike

我一直在做ipad应用。在这个应用程序中我有几个观点。这是流程

欢迎屏幕 > 主屏幕 > 其余屏幕

我在所有屏幕的导航栏上应用了主页图标(按钮)。在任何屏幕上按下主页图标都会将用户带到主屏幕。我在Home类的viewDidLoad中写了如下代码

//**** Home button on navigation bar ****//
CGRect frame1 = CGRectMake(975.0, 4.0, 35, 35);
UIImage *buttonImage1 = [UIImage imageNamed:@"HomeIcon.png"];
UIButton *homeButton = [UIButton buttonWithType:UIButtonTypeCustom];

homeButton.frame = frame1;
[homeButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
homeButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
homeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[homeButton addTarget:self action:@selector(goHome:) forControlEvents:UIControlEventTouchUpInside];
[homeButton setBackgroundColor:[UIColor clearColor]];

[self.navigationController.navigationBar addSubview:homeButton];

这个按钮是功能性的。 goHome 是@selector 中应用的方法的名称。我想从主屏幕上删除此按钮并将其保留在其余屏幕上。我已经应用了几件事,但我不知道该怎么做。这看起来很简单,但我还是不明白。请指导..

问候电脑

最佳答案

在您的“主屏幕”viewDidAppear 方法中执行此操作:

for(UIView* view in self.navigationController.navigationBar.subviews)
{
if(view.tag == 10)
{
view.hidden = YES;
}
}

在您创建按钮的其他 View Controller 中,将主页按钮的标签设置为 10。

/**** Home button on navigation bar ****//
CGRect frame1 = CGRectMake(975.0, 4.0, 35, 35);
UIImage *buttonImage1 = [UIImage imageNamed:@"HomeIcon.png"];
UIButton *homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[homebutton setTag:10]; // Set tag to 10 or any value
homeButton.frame = frame1;
[homeButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
homeButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
homeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[homeButton addTarget:self action:@selector(goHome:) forControlEvents:UIControlEventTouchUpInside];
[homeButton setBackgroundColor:[UIColor clearColor]];

[self.navigationController.navigationBar addSubview:homeButton];

在你的 viewDidAppear: 中,另一个 View Controller:

for(UIView* view in self.navigationController.navigationBar.subviews)
{
if(view.tag == 10)
{
view.hidden = NO;
}
}

关于objective-c - 如何从导航栏中删除按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8008605/

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