IBDesignable - custom button image doesn’t respect content alignment

Originator:openradar.rond
Number:rdar://20200486 Date Originated:3/17/15
Status:Open Resolved:
Product:Xcode Product Version:6.1.1; 6.3b3
Classification:Developer Tools Reproducible:Always
 
Summary:
In an IBDesignable subclass of UIButton, when calling setImage: programmatically …
if the frame size in IB is set larger than the image, the image does NOT respect the horizontal and vertical alignment properties set in IB.
(This is true BOTH in IBDesignable as well as at Runtime.)


Sample code, from subclass "CustomButton : UIButton”:

IB_DESIGNABLE
@interface CustomButton ()
@property (nonatomic, strong) IBInspectable NSString *buttonStyle;
@end

-(void)setButtonStyle:(NSString *)styleName {
    if ([styleName isEqualToString:@“play"]) {
        UIImage *foregroundImage = [UIImage imageNamed:(@“icon-play")
                       inBundle:[NSBundle bundleForClass:[self class]]
                compatibleWithTraitCollection:nil];

        [self setImage:foregroundImage forState:UIControlStateNormal];
    }
}

Steps to Reproduce:
In Interface Builder ...
- Add a “Button” from the Object library to a view controller’s view
- Change its Class to a custom subclass containing the IBInspectable code below
- Add a graphic asset by the referenced name to the project’s Images.xcassets 
- In IB’s Attributes Inspector, change the Button Style to the string value referenced in the code
- Set the Control Alignment to something *other* than the default values (left/top)
- Stretch the frame of the button in the canvas to larger than the icon’s actual size
- Once IBDesignable updates, observe that the image is aligned or stretched along the left-hand edge, and doesn’t respect the content alignment values (either within IBDesignable or at runtime).

[Please see attached images.  Note that the tinting of the image is a separate issue (#20200040).  To work around it temporarily, type in a dummy string value for the ‘image’ property in the Attributes inspector, then re-expand the button’s frame size in the canvas.]


Expected Results:
The image should align according to the .contentHorizontalAlignment and .contentVerticalAlignment properties set in IB.


Actual Results:
The image is aligned or stretched along the left-hand edge, and doesn’t respect the content alignment values set in IB (either within IBDesignable or at runtime).

Version:
OS X 10.9.5, Xcode 6.1.1; OS X 10.10.2, Xcode 6.3b3


Attachments:
'button-image-alignment1a.png', 'button-image-alignment1b.png' and 'button-image-alignment1c.png' were successfully uploaded.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!