iOS -- 解决iOS11中navigationBar上使用initWithCustomView按钮图片错位 frame无效

时间:2023-03-09 22:06:38
iOS -- 解决iOS11中navigationBar上使用initWithCustomView按钮图片错位 frame无效

在iOS11上当使用如下代码设置时

  1. UIButton *shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
  2. shareButton.frame = CGRectMake(0, 0, 30, 30);
  3. shareButton.backgroundColor = [UIColor blueColor];
  4. [shareButton setImage:[UIImage imageNamed:@"mv_actionIconSaveToAlbum"]  forState:(UIControlStateNormal)];
  5. UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:shareButton];
  6. self.navigationItem.rightBarButtonItem = rightBtnItem;

如果图片大小大于按钮设置的大小 ,设置的button的frame将不起作用。系统将采用最大区域展示。图片大小小于设置的button的frame时 不会出现这个问题。