Change width of a UIBarButtonItem in a UINavigationBar

前端 未结 6 1634
囚心锁ツ
囚心锁ツ 2020-12-01 12:10

I am creating a UIBarButtonItem and adding it to my navigation bar like so:

(void)viewDidLoad { 

   ...

   // Add the refresh button to the navigation bar
         


        
6条回答
  •  半阙折子戏
    2020-12-01 13:07

    The following approach worked, see code to change width and height of the button and to add action item as well.

    UIButton *imageButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
    [imageButton setBackgroundImage:[UIImage imageNamed:@"message_button"] forState:UIControlStateNormal];
    [imageButton addTarget:self action:@selector(messageButtonTapped) forControlEvents:UIControlEventAllEvents];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    

    Note: The target and action of the UIBarButtonItem does not apply to image view

提交回复
热议问题