Talha Ahmad Khan
Talha Ahmad Khan

Reputation: 3476

iOS - BarButtonItem Corner spacing in UINavigatioItem

I am creating RightBarButtonItem in UINavigationItem, Programmatically. Using this simple code

UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[backButton setImage:[UIImage imageNamed:@"concate_back_icon.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

Which Produces correct results, but there is an issue, There is little space between the botton and screen corner, which I don't want.

1st image

For that purpose I add spacing in button image, using edge insets

[backButton setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0,10)];

Which get me good visual results but the clickable area remained the same and So user misses the click most of the times.

Image 2

And now I am stuck here. Kindly let me know if there is another way to changing the corner space of bar button item. I have to stick with navigationItem.

Upvotes: 2

Views: 66

Answers (1)

Yuvaraj.M
Yuvaraj.M

Reputation: 9801

Just try to set the right edge insets to the barbutton based on your expectation. It works for me. Please let me know if you face issues. Thanks.

let rightBarButton = UIButton(type: .custom)
rightBarButton.setImage(UIImage.init(named: "Settings"), for: .normal)
rightBarButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
rightBarButton.addTarget(self, action: #selector(someAction), for: .touchUpInside)
rightBarButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -30.0) // Right edge insets
let rightBar = UIBarButtonItem(customView: rightBarButton)
self.navigationItem.rightBarButtonItem  = rightBar

Upvotes: 0

Related Questions