0

how do I reduce the distance between the two items?

my code:

-(void)setupRightMenuButton{
    filtro = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon-filtro-bar"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonFilter)];
    busca = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon-busca"] style:UIBarButtonItemStylePlain target:self action:@selector(moveToSearchView)];

    filtro.tintColor = [UIColor whiteColor];
    busca.tintColor = [UIColor whiteColor];

    filtro.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
    busca.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);

    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:busca, filtro, nil] animated:YES];
}

My viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupLeftMenuButton];

    [self.navigationController.navigationBar setBarTintColor: [UIColor colorWithRed:0.49 green:0.075 blue:0.082 alpha:1]]; /*#7d1315*/
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                         forBarMetrics:UIBarMetricsDefault];
    [self setupRightMenuButton];
    self.title = @"Músicas";

}

I tried using the filtro.imageInsets = UIEdgeInsetsMake (6, 0, -6, 0); but it did not work

Felipe Xavier
  • 165
  • 2
  • 13

2 Answers2

0

Try to add buttons this way:

    UIButton *but1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, width, height)];
    UIView *but1View = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [but1View addSubview:but1];
    UIBarButtonItem *rightButton1 = [[UIBarButtonItem alloc] initWithCustomView:but1View];

    UIButton *but2 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, width, height)];
    UIView *but2View = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [but2View addSubview:but2];
    UIBarButtonItem *rightButton2 = [[UIBarButtonItem alloc] initWithCustomView:but2View];

       self.navigationItem.rightBarButtonItems=@[ rightButton,rightButton2];

you need to mention the postions of x and y for the both UIViews as where ou want your buttons on the nav bar.

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
0

This code works for me. Try to create a button, then set the image to it and set the buttons frame.

let button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
let image = UIImage(named: "your_image")!
button.setImage(image , forState: UIControlState.Normal)
// optional -------------
button.addTarget(self, action: "your_function", forControlEvents: UIControlEvents.TouchUpInside)
// ------------
button.frame = CGRectMake(0, 0, image.width, image.height)
let barButton = UIBarButtonItem(customView: button)

Create as many barButtons as you want and then add them to navigationItem:

self.navigationItem.setRightBarButtonItems([barBtn1, barBtn2], animated: false)
mkz
  • 2,302
  • 2
  • 30
  • 43