setting image for UIBarButtonItem - image stretched

前端 未结 6 1852
北荒
北荒 2020-12-03 13:31

When I try to use UIBarButtonItem\'s \"initWithImage\" to initialize a navigation bar custom image, it comes out washed-up and stretched against a black navigation bar. This

6条回答
  •  伪装坚强ぢ
    2020-12-03 14:15

    For those who have come across this toolbar item stretching issue in iOS 11 specifically, it appears that the @2x version of your image is now required to render its frame and or bounds.

    So if you have code like this where you're adding a custom image UIBarButtonItem like this:

    UIButton *tagsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    tagsBtn.bounds = CGRectMake( 0, 0, 40, 40);
    [tagsBtn setImage:[UIImage imageNamed:@"tags.png"] forState:UIControlStateNormal];
    tags = [[UIBarButtonItem alloc] initWithCustomView:tagsBtn];
    [tagsBtn addTarget:self action:@selector(tags:) forControlEvents:UIControlEventTouchUpInside];
    
    [bottomToolbar setItems:[NSArray arrayWithObjects:flexibleSpace,tags,flexibleSpace,nil]];
    

    Then you will need to have a tags@2x.png that's 80x80, even if your tags.png image is 80x80. Simply renaming tags.png to tags@2x.png would resize the image to 40x40 as it did pre iOS 11 without changing code, or just add tags@2x.png to your project.

提交回复
热议问题