【问题标题】:How to remove a button from navigation bar?如何从导航栏中删除按钮?
【发布时间】:2011-11-04 11:31:33
【问题描述】:

我一直在研究 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 中应用的方法的名称。 我想从主屏幕中删除此按钮并将其保留在其余屏幕上。我已经应用了几件事,但我不知道该怎么做。这似乎很简单,但我仍然没有得到它。请指导..

问候 电脑

【问题讨论】:

    标签: objective-c ios ipad


    【解决方案1】:

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

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

    在您为主页按钮创建按钮的其他视图控制器中,将标签设置为 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: 这个其他视图控制器中:

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

    【讨论】:

      【解决方案2】:

      试试这个:

      self.navigationItem.backBarButtonItem = nil;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-19
        • 2020-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-06
        相关资源
        最近更新 更多