How to remove padding next to a UIBarButtonItem

后端 未结 4 1425
说谎
说谎 2021-01-05 13:46

I have added a custom button to the navigation bar\'s custom right bar button item as shown in the image.

I\'m want to be able to remove the space after the button

4条回答
  •  孤城傲影
    2021-01-05 14:04

    You can do this by adding a UIView then add inside it your button with x=spaceWidth y = 0.

    look at the below code for the right item (this is inside UINavigationItem category)

    1 -

    UIView *_rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)]; [_rightView setBackgroundColor:[UIColor purpleColor]];//debug color

    2-

        UIButton *aLeftButton = [[UIButton alloc] init];
    
       //...//
    
        aLeftButton.frame = CGRectMake(-15, 0, 44, 44);//in your case +15
        [aLeftButton setBackgroundImage:[UIImage imageNamed:buttonName] forState:UIControlStateNormal];
        [aLeftButton setBackgroundImage:[UIImage imageNamed:[buttonName stringByAppendingString:@"Selected"]] forState:UIControlStateSelected];
        [aLeftButton setAdjustsImageWhenDisabled:NO];
        [aLeftButton setAdjustsImageWhenHighlighted:NO];
    
        [aLeftButton addTarget:theSender action:aSelector forControlEvents:UIControlEventTouchUpInside];
        [_rightView addSubview:aLeftButton];
        UIBarButtonItem *aBarButton = [[UIBarButtonItem alloc] initWithCustomView:_rightView];
        [leftBarButtonsItem addObject:aBarButton];`
    

    3

    self.leftBarButtonItem = leftBarButtonsItem;

提交回复
热议问题