How to remove padding next to a UIBarButtonItem

后端 未结 4 1457
说谎
说谎 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条回答
  •  Happy的楠姐
    2021-01-05 14:16

    You can add a fixed space element with a negative width (I know, this sounds like a hack, but it works):

    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];    
    aButton.frame = CGRectMake(50.0, 0.0, 60, 44);
    UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
    [aButton addTarget:self action:@selector(testButtonControl:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *spaceFix = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:NULL];
    spaceFix.width = -5;
    
    self.navigationItem.rightBarButtonItems = @[spaceFix, aBarButtonItem];
    

提交回复
热议问题