0

I have 2 navigation bar items, but the size is too big, I want to make it smaller. how do i manage the size of them? should i edit them in the interface builder or should i manage them programmatically in viewDidLoad ?
enter image description here

I had given all the image size needed in the image asset, but it doesnt work

enter image description here

Agung Laksana
  • 781
  • 1
  • 12
  • 26
  • can you test in ios 11? – Sagar Bhut Feb 05 '18 at 07:01
  • see this https://stackoverflow.com/questions/32815490/ios-how-to-set-the-proper-image-scale-for-a-bar-button-item – Anbu.Karthik Feb 05 '18 at 07:01
  • Possible duplicate of [navigation bar rightbaritem image-button bug iOS 11](https://stackoverflow.com/questions/44442573/navigation-bar-rightbaritem-image-button-bug-ios-11) – Jayesh Thanki Feb 05 '18 at 07:02
  • 1
    mostly this would happen if the size of your images is incorrect, make sure you have the images in correct resolution according to apple HUI guidelines https://developer.apple.com/ios/human-interface-guidelines/icons-and-images/custom-icons/ – Geet Feb 05 '18 at 07:41

2 Answers2

3

You have to take custom view for the navigationItem

take a button and give frame as per your requirement

set an image to the button

assign a button as customView to UIBarButtonItem

try following code ..it may help you

    let customButton = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 30, height: 30))
    customButton.setImage(UIImage.init(named:"imageName"), for: .normal)
    self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: customButton)

Thank you

VishalPethani
  • 854
  • 1
  • 9
  • 17
0

To use resized images in bar buttons, bellow code will help you:

UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
[customView setBackgroundColor:[UIColor clearColor]];

CGRect iconRect = CGRectMake(((40 - 28)/2), ((40 - 28)/2), 28, 28);
UIImageView *imageIcon = [[UIImageView alloc] initWithFrame: iconRect];
imageIcon = [UIImage imageNamed:imageName];
[customView addSubview: imageIcon];

UIButton *tranparentButton = [UIButton buttonWithType:UIButtonTypeCustom];
[subscribeButton setFrame:CGRectMake(0, 0, 40, 40)];
[customView addSubview: tranparentButton];
[tranparentButton addTarget:self action:@selector(cancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView: customView];

self.navigationItem.rightBarButtonItem = rightBarButton;

Thanks

Usman Nisar
  • 3,031
  • 33
  • 41