This is the 19th day of my participation in the August Gwen Challenge. For details: August Gwen Challenge “juejin.cn/post/698796…”

preface

Example: Adjust the spacing between the BarButtonItem button and titleView

Principle: The starting position and size of the TitleView depend on the position of leftBarButtonItem and rightBarButtonItem.

2. Before setting the TitleView, initialize the position of leftBarButtonItem and rightBarButtonItem, and center the TitleView according to the position of leftBarButtonItem and rightBarButtonItem.

Q&A

1. BarButtonItem hiding failure solution: instantiate BarButtonItem with initWithCustomView

2. IOS13.5.1 version cannot click the button on the right of the navigation bar: CustomView cannot be directly UIButton, so the solution is as long as UIButton is packaged as CustomView

I. Adjust the spacing between the BarButtonItem button and titleView

Spacing with screen boundaries or titleView by adjusting the order of the rightBarButtonItems array elements separately.

The main use of UIBarButtonItem UIBarButtonSystemItemFixedSpace system controls

 UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    /** * if width is negative, BTN moves width to the right by pixels. If width is set to -5, BTN moves width by pixels. When width is positive, the opposite is true, which is equivalent to moving width to the left by pixels */
    negativeSpacer.width = 10;

Copy the code

1.1 Adjust the spacing between the right button and titleView


 // Set the right button
    UIBarButtonItem *btn_right =  [UIBarButtonItem barButtonItemWithTarget:self  Image:@"in" highlightedImage:@"in" actionMethod:@selector(in)];
    
    
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    /** * if width is negative, BTN moves width to the right by pixels. If width is set to -5, BTN moves width by pixels. When width is positive, the opposite is true, which is equivalent to moving width to the left by pixels */
    negativeSpacer.width = 10;
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects: btn_right, negativeSpacer,nil];
Copy the code

1.2 Adjust the spacing between the left button and titleView


// The titleView's starting position and size depend on the position of leftBarButtonItem and rightBarButtonItem.
    

        UILabel * centerView = [[UILabel alloc] initWithFrame:CGRectMake(kAdjustRatio(20),0 , self.view.frame.size.width- kAdjustRatio(20), self.navigationController.navigationBar.frame.size.height)];
    
    self.navigationItem.titleView = centerView;
    
Before setting the TitleView, initialize the position of leftBarButtonItem and rightBarButtonItem, and center the TitleView according to the position of leftBarButtonItem and rightBarButtonItem.
    

// self.navigationItem.titleView.
    
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    /** * if width is negative, BTN moves width to the right by pixels. If width is set to -5, BTN moves width by pixels. When width is positive, the opposite is true, which is equivalent to moving width to the left by pixels */

    negativeSpacer.width = 10;

    
// self.navigationItem.leftBarButtonItem =negativeSpacer;

    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:  negativeSpacer,nil];

Copy the code

1.3 Set the position of the left button of the webViewVC navigation bar

- (void)updateNavigationItems {
    [self.navigationItem setLeftBarButtonItems:nil animated:NO];
    if (self.webView.canGoBack/* || self.webView.backForwardList.backItem*/) {// Web view can go back means a lot requests exist.
        UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        spaceButtonItem.width = 6.5;
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        if (self.navigationController.viewControllers.count == 1) {
            NSMutableArray *leftBarButtonItems = [NSMutableArray arrayWithArray:@[spaceButtonItem,self.navigationBackBarButtonItem]];
            // If the top view controller of the navigation controller is current vc, the close item is ignored.
            if (self.showsNavigationCloseBarButtonItem && self.navigationController.topViewController ! =self){
                [leftBarButtonItems addObject:self.navigationCloseBarButtonItem];
            }
            
            [self.navigationItem setLeftBarButtonItems:leftBarButtonItems animated:NO];
        } else {
            if (self.showsNavigationCloseBarButtonItem){
                [self.navigationItem setLeftBarButtonItems:@[self.navigationCloseBarButtonItem] animated:NO];
            }else{[self.navigationItem setLeftBarButtonItems:@[] animated:NO]; }}}else {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        [self.navigationItem setLeftBarButtonItems:nil animated:NO]; }}Copy the code

II. Related frequently asked Questions

2.1 self.navigationItem.leftBarButtonItem.customView.hidden=YESfailure

  • UIBarButtonItem useinitWithCustomViewThis method only takes effect when instantiated
self.navigationItem.leftBarButtonItem.customView.hidden=YES
Copy the code
  • Solution: UseinitWithCustomViewInstantiate


 UIBarButtonItem *lefttItem = [[UIBarButtonItem alloc]initWithCustomView:btn];

    
    self.navigationItem.leftBarButtonItem = lefttItem;

Copy the code

2.2 iOS13.5.1 failure to click the button on the right of the navigation bar

  • Unable to click code
// UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
// [btn setTitle:QCTLocal(@"print") forState:UIControlStateNormal];
// [btn setTitleColor:HWColor(51, 51, 51) forState:UIControlStateNormal];
Font = [UIFont systemFontOfSize:14.0f]; // btn.titlelabel. font = [UIFont systemFontOfSize:14.0f];
// [btn addTarget:self action:@selector(printBtn:) forControlEvents:UIControlEventTouchUpInside];
// btn.tag = 0;
// btn.tintColor = [UIColor blackColor];
// UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
// rightBtn.tintColor = [UIColor blackColor];
// self.navigationItem.rightBarButtonItem = rightBtn;

Copy the code
  • The solution

CustomView cannot be UIButton directly, so the solution is simply to wrap UIButton one layer

- (void)setupNavigationBar {
    // Set the button on the right of the navigation bar
    UIButton *btn = [[UIButton alloc] init];
    // [btn setImage:[UIImage imageNamed:@"icon_kaidan_huiyuan"] forState:UIControlStateNormal];
    [btn setTitle:QCTLocal(@"print") forState:UIControlStateNormal];
    [btn setTitleColor:rgb(51.51.51) forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(printBtn:) forControlEvents:UIControlEventTouchDown];
    btn.titleLabel.font = kTextFont(14);
    [btn sizeToFit];
    UIView *btnView = [[UIView alloc] init];
    btnView.frame = btn.bounds;
    [btnView addSubview:btn];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btnView];
    item.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = item;
    
    
    
    
    
}


Copy the code

2.3 Use [UIBarButtonItem alloc] initWithImage: method, resulting in image color washed solution

Back to the current controller from other VC, found the self on the right. The navigationItem. The background color of the rightBarButtonItem been watered down

  • The solution: UIImageRenderingModeAlwaysOriginal
    UIImage *aimage = [UIImage imageNamed:@"icon_right_menu"];
    UIImage *image = [aimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(moreAction)];
    [self.navigationItem setRightBarButtonItem:rightItem animated:YES];



Copy the code

III. Details of controller jump and rollback

3.1 UIActionSheet opened the photo album of processing: usually jump didDismissWithButtonIndex before such as controller

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 8_3) __TVOS_PROHIBITED{
    
    if (buttonIndex == 0) {[selfcreateCamera]; }}Copy the code

3.2 POP back the VC specified in the stack

- (void)pop
{
    for (UIViewController *temp in self.navigationController.viewControllers) {
        if ([temp isKindOfClass:[QCTMyFinancialViewController class{[]])self.navigationController popToViewController:temp animated:YES]; }}}Copy the code

IV, see also

Thank you for your support, please pay attention to the public number: iOS reverse

Common extension methods

UIBarButtonItem (Extension)

@implementation UIBarButtonItem (Extension)
+ (UIBarButtonItem*)barButtonItemWithTarget:(id)target Image:(NSString*)imageName highlightedImage:(NSString*)highlightedImage actionMethod:(SEL)actionMethod{
    // Set the image
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:highlightedImage] forState:UIControlStateHighlighted];
    / / set the frame
    button.size = button.currentBackgroundImage.size;
    
    // Set the listener
    [button addTarget:target action:actionMethod forControlEvents:UIControlEventTouchUpInside];
    return  [[UIBarButtonItem alloc]initWithCustomView:button];
}


Copy the code