简体   繁体   English

进入视图时,撰写UIBarButtonItem稍微改变位置

[英]Compose UIBarButtonItem changes position slightly when coming into view

When presenting a new view with a UIBarButtonSystemItemCompose button in the navigation bar, the position is slightly off and adjusts after the view has come into view. 在导航栏中使用UIBarButtonSystemItemCompose按钮呈现新视图时,位置略微偏离,并在视图进入视图后进行调整。

进入视图时,撰写按钮会稍微改变位置

I think this is a bug in iOS (version 8.3 used). 我认为这是iOS中的一个错误(使用的是8.3版本)。 It only happens when using the UIBarButtonSystemItemCompose. 它只在使用UIBarButtonSystemItemCompose时发生。 It does not happen with other types of Buttons (system, text or custom). 其他类型的按钮(系统,文本或自定义)不会发生这种情况。

The only code needed to replicate this bug is to use this ViewController code with the view that will come into view: 复制此错误所需的唯一代码是将此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

I have created a repository on GitHub with bare minimum code to reproduce the problem: https://github.com/jvdvleuten/iOSComposeBarButtonItemBug 我在GitHub上创建了一个存储库,只有最少的代码来重现问题: https//github.com/jvdvleuten/iOSComposeBarButtonItemBug

Looks related to this: UIBarButtonItems shift position when UINavigationController is presented modally , except my bug only appears when using the UIBarButtonSystemItemCompose. 看起来与此相关: 当UINavigationController以模态方式呈现时,UIBarButtonItems移位 ,除了我的bug只在使用UIBarButtonSystemItemCompose时出现。

Any ideas? 有任何想法吗?

I used Sergey's answer, but kept an empty space right of my button. 我使用谢尔盖的答案,但在我的按钮右边保留了一个空的空间。 I fixed this with a negative spacer, which now works beautifully: 我用一个负间隔物固定它,现在可以很好地工作:

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];

This is definitely a bug in iOS 8.0. 这绝对是iOS 8.0中的一个错误。 This 'jump' occurs before viewDidAppear . 这个“跳跃”发生在viewDidAppear之前。 Here is workaround for this - add another 'dumb'/empty item: 这是解决方法 - 添加另一个'哑'/空项:

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

A simple workaround: 一个简单的解决方法:

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

We can get the image 'UIButtonBarCompose' with iOS-Artwork-Extractor . 我们可以使用iOS-Artwork-Extractor获取图像'UIButtonBarCompose'。

I think this is problem of UIBarButtonSystemItemCompose . 我认为这是UIBarButtonSystemItemCompose问题。 need some correction from apple developer team. 需要苹果开发团队的一些修正。 Untill apple don't get resolve this bug. 直到苹果不解决这个错误。 you can create your custom button and set it to rightBarButtonItem using following code. 您可以使用以下代码创建自定义按钮并将其设置为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");
}

Hope this help you. 希望这对你有所帮助。

Nice observation , This problem solved in viewDidAppear . 很好的观察 ,这个问题在viewDidAppear解决了。 Can you please check this.. 你能来看看这个..

- (void)viewDidAppear:(BOOL)animated{    
        [super viewDidAppear:animated];

        UIBarButtonItem* composeBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:nil action:nil];    
        [self.navigationItem setRightBarButtonItem:composeBarButtonItem animated:YES];    
}

This might help you :) 这可能会帮助你:)

Yes, this is IOS8 bug. 是的,这是IOS8的错误。

It happens because jumping not a navigation bar item position, its jumping image position inside compose item. 这是因为跳转不是导航栏项目位置,其跳跃图像位置在组合项目内。 This item type seems hasn't override for push animation or something similar, as for example back button. 此项类型似乎没有覆盖推送动画或类似的东西,例如后退按钮。

I think you should create bug on radar and for fix your current trouble, just create custom UIBarButtonItem with the same image. 我认为您应该在雷达上创建bug并修复当前的麻烦,只需使用相同的图像创建自定义UIBarButtonItem。

it might happen of animation 它可能发生在动画中

try this one. 试试这个。 [self.navigationItem setRightBarButtonItem:composeBarButtonItem animated:NO]; [self.navigationItem setRightBarButtonItem:composeBarButtonItem animated:NO];

hopefully helped :) 希望有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将视图约束到UIBarButtonItem的位置 - Constrain a view to position of a UIBarButtonItem iOS自定义UIBarbuttonItem在详细视图中更改位置 - IOS Custom UIBarbuttonItem change position in detail view 点击时,iOS 7 UIBarButtonItem字体会发生变化 - iOS 7 UIBarButtonItem font changes when tapped 更改UIBarButtonItem的外观还会更改导航栏和工具栏的位置吗? - Changing appearance of UIBarButtonItem also changes position of navigationbar and toolbar? 在带有UIGestureRecognizer的视图中不调用UIBarButtonItem的操作 - UIBarButtonItem's action is not called when in a view with a UIGestureRecognizer 为什么视图在使用CGAffineTransformMakeRotation旋转时会改变位置 - Why does a view changes position when rotating it using CGAffineTransformMakeRotation 动画帧时如何获取视图位置的变化? - How to obtain changes to view position when animating frame? 表格视图单元格复选标记在滚动时更改位置 - Table view cell checkmark changes position when scrolling UITableview从上一个视图返回时更改Y位置 - UITableview Changes Y position when returned from previous view UIBarButtonItem位置在通过情节提要板还是通过编程方式放置时有所不同 - UIBarButtonItem position different when placed via storyboard vs. programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM