Compose UIBarButtonItem changes position slightly when coming into view

后端 未结 7 2048
星月不相逢
星月不相逢 2021-02-13 15:21

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.

相关标签:
7条回答
  • 2021-02-13 15:58

    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];
    
    0 讨论(0)
  • 2021-02-13 15:58

    it might happen of animation

    try this one. [self.navigationItem setRightBarButtonItem:composeBarButtonItem animated:NO];

    hopefully helped :)

    0 讨论(0)
  • 2021-02-13 16:07

    I think this is problem of 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.

    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.

    0 讨论(0)
  • 2021-02-13 16:08

    Yes, this is IOS8 bug.

    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.

    0 讨论(0)
  • 2021-02-13 16:12

    Nice observation, This problem solved in 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 :)

    0 讨论(0)
  • 2021-02-13 16:16

    This is definitely a bug in iOS 8.0. This 'jump' occurs before 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];
    
    0 讨论(0)
提交回复
热议问题