William Falcon
William Falcon

Reputation: 9823

How to place UINavigationBarButton in custom location

My current navigation bar looks like this:

enter image description here

As you can see I need to move it to the right. Not sure that I'm setting this up correctly, any suggestions?

Here is the code:

-(void)customizeNavigationBar{

    //*** Nav bar Background ***
    //Image being used
    UIImage *backgroundImage = [UIImage imageNamed:@"navBarBackground"];

    //Set navigation bar image
    [self.navigationController.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];

    //*** Search button**
    //Get Image
    UIImage* searchImage = [UIImage imageNamed:@"navSearchButton"];

    //Set frame
    CGRect frame = CGRectMake(0, 0, searchImage.size.width, searchImage.size.height);

    //Create button
    UIButton *searchButton = [[UIButton alloc] initWithFrame:frame];

    //Set background images (selected and not)
    [searchButton setBackgroundImage:searchImage forState:UIControlStateNormal];
    [searchButton setBackgroundImage:[UIImage imageNamed:@"navSearchButtonSelected"] forState:UIControlStateSelected];

    //Add action
    [searchButton addTarget:self action:@selector(search)
          forControlEvents:UIControlEventTouchUpInside];

    //Set highlight on
    [searchButton setShowsTouchWhenHighlighted:YES];

    //Create nav buttons
    UIBarButtonItem *navSearchBtn =[[UIBarButtonItem alloc] initWithCustomView:searchButton];

    //Add to nav bar
    [self.navigationItem setLeftBarButtonItem:navSearchBtn];
}

Upvotes: 0

Views: 61

Answers (1)

rudensm
rudensm

Reputation: 1474

  • Use setImage:forState: for setting background image to UIButton (instead of setBackgroundImage:)
  • Set UIButton's frame a bit larger (size.width)

Upvotes: 1

Related Questions