Change position of UIBarButtonItem in UINavigationBar

后端 未结 20 1577
一个人的身影
一个人的身影 2020-11-27 10:31

How can I change the position of a UIBarButtonItem in a UINavigationBar? I would like my button to be about 5px higher than its normal position.

20条回答
  •  孤街浪徒
    2020-11-27 11:03

    I found the solution of this problem by making adjustment in the Image Edge Insets of the custom button. I had the requirement in the app to increase the height of the navigation bar and after increasing the height makes the rightBarButtonItem and leftBarButtonItem images unpositioned problem.

    Find the code below:-

    UIImage *image = [[UIImage imageNamed:@"searchbar.png"];
    UIButton* searchbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchbutton addTarget:self action:@selector(searchBar:) forControlEvents:UIControlEventTouchUpInside]; 
    searchbutton.frame = CGRectMake(0,0,22, 22);
    [searchbutton setImage:image forState:UIControlStateNormal];
    [searchbutton setImageEdgeInsets:UIEdgeInsetsMake(-50, 0,50, 0)];
    // Make BarButton Item
     UIBarButtonItem *navItem = [[UIBarButtonItem alloc] initWithCustomView:searchbutton];
    self.navigationItem.rightBarButtonItem = navItem;
    

    Hope this helps anyone.

提交回复
热议问题