UIBarButtonItems shift position when UINavigationController is presented modally

前端 未结 3 1744
迷失自我
迷失自我 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:39

    I was presenting from a detached viewController. I never got the warning. But I tried embedding that detached viewController in a NavigationController, and the problem went away.

    0 讨论(0)
  • 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];
    <other button config here, set targets, whatever>
    [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.

    0 讨论(0)
  • 2021-02-19 11:58

    As @Stonz2 mentioned, this appears to be a characteristic of presenting a modal from a detached view controller. I was having the same problem and remedied it by re-organizing my app so that it wasn't presenting from a detached controller.

    You'll know if you're presenting from a detached controller if you get the following error message:

    Presenting view controllers on detached view controllers is discouraged

    0 讨论(0)
提交回复
热议问题