setBackgroundImage for UIBarButtonItem via appearance not working for other UIControlStates

本秂侑毒 提交于 2020-01-03 03:29:05

问题


I'm trying to customize a UIBarButtonItem using the appearance method (>iOS 5.0). It works fine for the UIControlStateNormal, but not for highlighted or disabled. See images

Here's the code I use to set those:

        // now configure the UIBarButtonItems
    UIImage *buttonBGInactive = [[UIImage imageNamed:@"button-navbar-30-inactive.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 15)];
    UIImage *buttonBGActive = [[UIImage imageNamed:@"button-navbar-30-pressed.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 15)];

    [[UIBarButtonItem appearanceWhenContainedIn:[MFMailComposeViewController class], nil] setBackgroundImage: buttonBGInactive forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearanceWhenContainedIn:[MFMailComposeViewController class], nil] setBackgroundImage: buttonBGActive forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

Any ideas? The images themselves are 30x30px and I'm not requiring landscape mode.


回答1:


I found the solution to the problem. My images are 30x30px and I set the resizable endcaps to

UIEdgeInsetsMake(0, 15, 0, 15)

by setting those 15's to 14's, the problem went away. So best not to make endcaps the exact half of the image width.



来源:https://stackoverflow.com/questions/12301555/setbackgroundimage-for-uibarbuttonitem-via-appearance-not-working-for-other-uico

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!