Smaller active area for custom UIBarButtonItem

后端 未结 3 1891
面向向阳花
面向向阳花 2021-02-10 01:17

I have a UINavigationBar with a custom UIBarButtonItem (which uses a UIButton as its custom view). The problem is: the active area of the custom button is much too large, if I t

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-10 01:49

    I noticed this weirdness too. I found that using a container UIView fixes this. For example:

    UIButton *menuButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [menuButton addTarget:self action:@selector(revealMenu:) forControlEvents:UIControlEventTouchUpInside];
    [menuButton setImage:[UIImage imageNamed:@"menuIcon"] forState:UIControlStateNormal];
    UIView *menuButtonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [menuButtonContainer addSubview:menuButton];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuButtonContainer];
    

提交回复
热议问题