【问题标题】:rightbarbuttonitem not displayingrightbarbuttonitem 不显示
【发布时间】:2011-06-23 14:23:41
【问题描述】:

我有以下代码用于放置 rightbarbuttonitem

UIButton* rightbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightbutton setBackgroundImage:[UIImage imageNamed:@"share-icon.png"] forState:UIControlStateNormal];
    [rightbutton addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:rightbutton] autorelease];

但它没有显示任何 barbuttonitem。相反,如果我使用以下代码,则会出现 barbutton 项目,但问题是我无法在 barbuttonitem 上使用此代码设置触摸事件。

UIImageView *iconView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dashboard-icon.png"]];
    UIBarButtonItem *icon = [[UIBarButtonItem alloc] initWithCustomView:iconView];
    self.navigationItem.leftBarButtonItem=icon;
    [icon release];
    [iconView release];

【问题讨论】:

    标签: iphone ios ios4


    【解决方案1】:

    我的猜测是,您将UIBarButtonItem 添加到了错误的对象! 您需要将它添加到 rootViewController(而不是像您可能所做的那样添加到 UINavigationController

    YourRootViewController *theRootController = [[YourRootViewController alloc] init];
    
    UINavigationController* navContainer = [[UINavigationController alloc] initWithRootViewController:theRootController];
    
    UIButton* rightbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightbutton setBackgroundImage:[UIImage imageNamed:@"share-icon.png"] forState:UIControlStateNormal];
        [rightbutton addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
     theRootController.navigationItem.rightBarButtonItem = rightbutton;
    
    [navContainer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentModalViewController:navContainer animated:YES];
    

    【讨论】:

      【解决方案2】:

      如果您使用白色导航栏,请不要忘记在 UIBarButtonItem 上设置 tintColor。我的按钮在那里但不可见。

      【讨论】:

        【解决方案3】:

        您是否尝试为rightbutton 设置合适的框架?例如rightbutton.frame = (CGRect){CGPointZero, image.size};

        另请注意:

        在 iOS 4 及更高版本上, 文件不需要指定 文件扩展名。在 iOS 4 之前, 您必须指定文件名 扩展名。

        【讨论】:

        • 我的答案包含示例代码。您需要做的是声明一个 image 变量并将其设置为您的图像。
        【解决方案4】:

        使用 swift 3.0 iOS 10.0

        让 customButton = UIBarButtonItem(title: "Right", style: .plain, target: self, action: #selector(yeah)) customButton.width = 100.0 navigationItem.rightBarButtonItem = 自定义按钮

        func 是的(发件人:UIBarButtonItem){ 打印(“哟你的男人/女人”) }

        【讨论】:

          【解决方案5】:

          我在为孩子设置 rightBardButtonItems 时遇到了这个问题 而不是最根视图控制器

          视图控制器总是有导航项,但不一定 意味着它有一个导航栏来显示它

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-10-10
            • 2012-06-10
            相关资源
            最近更新 更多