UIBarButtonItems shift position when UINavigationController is presented modally

前端 未结 3 1758
迷失自我
迷失自我 2021-02-19 11:19

I\'m presenting a UINavigationController modally, from within an iOS app extension:

UINavigationController *nav = [[UINavigationController alloc] in         


        
3条回答
  •  心在旅途
    2021-02-19 11:50

    I'm hoping someone else finds a better way to do this, but my current solution is to create stand alone buttons and then use [[UIBarButtonItem alloc] initWithCustomView:] with the button. Not ideal, and the indention is still there, but the weird jump doesn't happen anymore.

    If the indention bothers you, you could also set negative fixed spaces on the outsides of the buttons to shift them closer to the edges.

    Here's an example including the spacer:

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [button sizeToFit];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    [fixedSpace setWidth:-8.0f];
    
    self.navigationItem.leftBarButtonItems = @[fixedSpace, leftBarButton];
    

    This only happens to me on my share extension, and I don't have any problems related to presenting from a detached controller or anything like that.

提交回复
热议问题