I am trying to set an image as the background of a custom UIButton. I was able to set a background image for the "rounded rect" UIButton in interface builder, but now the borders have vanished. Is there any way to retain the borders? Also, I would like to make the borders show up rectangular instead of rounded.
+4
A:
Check out the cornerRadius, borderWidth and borderColor properties of CALayer. These properties are new as of iPhone OS 3.0.
You probably want to subclass UIButton and then set these properties in the constructor. Your button has a layer property you can access to set these border properties.
Here's an example:
- (id)initWithFrame:(CGRect)frame {
if(self = [super initWithFrame:frame]) {
[self.layer setBorderWidth:1.0];
[self.layer setCornerRadius:5.0];
[self.layer setBorderColor:[[UIColor colorWithWhite:0.3 alpha:0.7] CGColor]];
}
return self;
}
Jonathan Arbogast
2009-05-12 19:29:33
Is there any workaround in iPhone 2.2.1?
afin
2009-05-13 02:06:10
In 2.2.1 I would go with something along the lines of what Kendall is suggesting below.Is this a new application? If so, I'm pretty sure that Apple will only be approving apps that are compatible with iPhone OS 3.0 from now on.
Jonathan Arbogast
2009-05-13 14:24:47
Yes, It is a new application. Since Apple is going to approve only OS 3.0 compatible applications, new applications may not work with previous OS versions. Even though OS 3.0 upgrade is free, some of them may not willing to upgrade. Please share your thoughts.
afin
2009-05-13 15:16:38
Because 3.0 is free (actually $10 for iPod Touch) the financial barrier for users to upgrade is pretty low. With all the new OS features and associated apps that will be developed, I think most users will upgrade to 3.0 fairly quickly.With those assumptions in place, I'd rather focus my time on improving my user experience and adding new features. I guess I want to keep my support/maintenance activities to a minimum as long as it doesn't greatly affect my users.
Jonathan Arbogast
2009-05-13 19:04:45
Thank you, let us hope most of the users will do upgrade.
afin
2009-05-13 20:20:48
Historically, the vast majority of users have upgraded... after all, you're not worrying about targeting 2.1 to capture all 12 users who haven't upgraded from that version.
mmc
2009-07-12 16:56:40
+2
A:
When you set a background image in a button, the button outlines go away. Include them in your image, if you need different sizes look at the UIImage
stretchableImageWithLeftCapWidth:topCapHeight:
method.
Kendall Helmstetter Gelner
2009-05-13 06:47:34