【问题标题】:Compose UIBarButtonItem changes position slightly when coming into viewCompose UIBarButtonItem 在进入视图时会稍微改变位置
【发布时间】:2015-04-22 11:01:39
【问题描述】:

在导航栏中显示带有 UIBarButtonSystemItemCompose 按钮​​的新视图时,位置会稍微偏离并在视图进入视图后进行调整。

我认为这是 iOS 中的一个错误(使用的是 8.3 版)。它仅在使用 UIBarButtonSystemItemCompose 时发生。其他类型的按钮(系统、文本或自定义)不会发生这种情况。

复制此错误所需的唯一代码是将此 ViewController 代码与将要显示的视图一起使用:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    UIBarButtonItem* composeBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
                                      target:nil
                                      action:nil];

    [self.navigationItem setRightBarButtonItem:composeBarButtonItem animated:YES];
}

@end

我在 GitHub 上创建了一个存储库,使用最少的代码来重现该问题:https://github.com/jvdvleuten/iOSComposeBarButtonItemBug

看起来与此相关:UIBarButtonItems shift position when UINavigationController is presented modally,除了我的错误仅在使用 UIBarButtonSystemItemCompose 时出现。

有什么想法吗?

【问题讨论】:

  • ya..当您使用 compose 作为样式时,这是一个错误...但我认为您应该使用 viewDiidAppear 来处理此代码...
  • 不,当您使用viewDidApear中的代码时,视图会在没有按钮的情况下进入视图。这不是标准的。
  • 是的,我知道......但它现在是一个解决方案......而不是那种恼人的观点......那是更好的解决方案......或者使用 sergey Kuryanov 的答案作为解决方案
  • 我现在正在使用撰写按钮的图像,它可以工作。我今天会试试 Sergey 的答案。
  • 好的...这是您使用 compose 图像的最后一个选项...我知道它的工作原理和魅力

标签: ios objective-c uinavigationcontroller uibarbuttonitem uibarbuttonsystemitem


【解决方案1】:

我使用了 Sergey 的答案,但在我的按钮右侧保留了一个空白区域。我用负垫片修复了这个问题,现在效果很好:

UIBarButtonItem* composeBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
                                              target:nil
                                              action:nil];

UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
negativeSpacer.width = -6;

UIBarButtonItem *dumbBarButtonItem = [UIBarButtonItem new];

self.navigationItem.rightBarButtonItems = @[dumbBarButtonItem, negativeSpacer, self.composeBarButtonItem];

【讨论】:

  • 嗯..我也使用了垫片,但它对我不起作用。也许是因为我只使用了垫片和原始按钮。很好的收获。
  • 我也不知道 -6 是否合适,但“感觉”是对的 ;)
【解决方案2】:

这绝对是 iOS 8.0 中的一个错误。这个“跳跃”发生在viewDidAppear 之前。这是解决此问题的方法 - 添加另一个“哑”/空项目:

UIBarButtonItem* composeBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
                                              target:nil
                                              action:nil];
UIBarButtonItem *dumbBarButtonItem = [UIBarButtonItem new];
self.navigationItem.rightBarButtonItems = @[dumbBarButtonItem, composeBarButtonItem];

【讨论】:

  • 我实际上在撰写按钮右侧保留了一个空白区域,并且无法通过空白视图摆脱它?
【解决方案3】:

一个简单的解决方法:

let composeButton = UIBarButtonItem(image: UIImage(named: "UIButtonBarCompose"), style: .Plain, target: self, action: "compose:")
self.navigationItem.rightBarButtonItem = composeButton

我们可以使用iOS-Artwork-Extractor 获取图像“UIButtonBarCompose”。

【讨论】:

    【解决方案4】:

    我认为这是UIBarButtonSystemItemCompose 的问题。需要苹果开发团队的一些修正。直到苹果没有解决这个错误。您可以使用以下代码创建自定义按钮并将其设置为rightBarButtonItem

    UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"compose.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
        [button setFrame:CGRectMake(0, 0, 53, 31)];
    
        UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
        self.navigationItem.rightBarButtonItem = barButton;
    
    -(void)buttonAction:(id)sender{
        NSLog(@"Click");
    }
    

    希望对您有所帮助。

    【讨论】:

    • 愿有人反对我的回答。我认为你对同样的问题有更好的答案。请发布您的答案,然后投反对票。将来可能会对我们有所帮助。
    【解决方案5】:

    很好的观察,这个问题在viewDidAppear 中解决了。你能检查一下吗..

    - (void)viewDidAppear:(BOOL)animated{    
            [super viewDidAppear:animated];
    
            UIBarButtonItem* composeBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:nil action:nil];    
            [self.navigationItem setRightBarButtonItem:composeBarButtonItem animated:YES];    
    }
    

    这可能对你有帮助:)

    【讨论】:

    • 这将使我的按钮动画太晚。
    【解决方案6】:

    是的,这是 IOS8 的错误。

    这是因为跳转不是导航栏项目位置,而是在撰写项目中的跳转图像位置。此项目类型似乎没有覆盖推送动画或类似的东西,例如后退按钮。

    我认为您应该在雷达上创建错误并解决您当前的问题,只需创建具有相同图像的自定义 UIBarButtonItem。

    【讨论】:

    • 我只建议相关的事情,并通过花时间调查来确认错误。下次我不会这样做了。
    • 我已向 Apple 添加了错误报告。感谢您的帮助。
    【解决方案7】:

    动画可能会发生

    试试这个。 [self.navigationItem setRightBarButtonItem:composeBarButtonItem Animation:NO];

    希望有所帮助:)

    【讨论】:

    • 试过了,结果一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多