Reputation: 75296
I've created a class that inherits from UIButton
. If I add rounded-rect buttons to my XIB and set their class property to UIButton
, they appear as rounded-rect buttons (but then none of my custom code is ever called, so the buttons behave like ordinary buttons which isn't what I want). If I set these buttons' class property in IB to my custom class UIButtonCustom
, they still appear to be rounded rect buttons in the IB designer, but when I run the app the buttons are of the custom type (meaning they appear mostly blank with the button text, since I'm not setting any background image for the button).
Is there any way to get these buttons to look like rounded rect buttons when the app actually runs?
Upvotes: 0
Views: 2038
Reputation: 31745
As others here have noted, you cannot subclass a UIRoundedRectButton. But you can get your buttons to look like rounded rect buttons by setting some CALayer properties in your init method.
#import "MyCustomButton.h"
#import <QuartzCore/QuartzCore.h>
@implementation MyCustomButton
- (id)initWithFrame:(CGRect)frame
//button created in code
{
self = [super initWithFrame:frame];
if (self) {
[self initialise];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder;
//button created in Interface Builder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self initialise];
}
return self;
}
- (void)initialise;
{
self.layer.cornerRadius = 10.0f;
self.layer.borderWidth = 1.0f;
self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
}
@end
If you have autolayout disabled you can continue to use 'Rounded Rect' button style in IB to give you the correct appearance in layout, although the style will be ignored when your custom button loads. If autolayout is enabled, you will have to change your IB style to 'custom', or you will find the button frame does not behave the way you expect.
Upvotes: 1
Reputation: 39988
The rounded button that you see on xib is sub class of UIButton
i.e. UIRoundedRectButton
.
And I don't think you can subclass it as it's not documented (a private API).
Upvotes: 1