开发者

Problems applying custom coded images to a Round Rect Button in Interface Builder

开发者 https://www.devze.com 2023-02-03 17:29 出处:网络
I\'m running through a few tutorials that describe ways of applying custom images to UIButtons in iPhone programming using Obj-C.

I'm running through a few tutorials that describe ways of applying custom images to UIButtons in iPhone programming using Obj-C.

One method i like is to place a UIButton (Round Rect Button) on a XIB using Interface Builder then in the 'viewDidLoad' method of the XIB's view controller skin the button thus:

UIImage *ButtonImageNormal = [UIImage imageNamed:@"button_blue.png"];
UIImage *StretchableButtonImageNormal = [ButtonImageNormal stretchableImageWithLeftCapWidth:24 topCapHeight:24];
[self.Button setBackgroundImage:StretchableButtonImageNormal forState:UIControlStateNormal];

UIImage *ButtonImagePressed = [UIImage imageNamed:@"button_orange.png"];
UIImage *StretchableButtonImagePressed = [ButtonImagePressed stretchableImageWithLeftCapWidth:24 topCapHeight:24];
[self.Button setBackgroundImage:StretchableButtonImagePressed forState:UIControlStateHighlighted];

The trouble with this method is that the button doesn't lose its 'Round Rect' background and at the side of the buttons you can still see the white of the 'Round Rect' button peeping through past the custom images.

If however, i create a custom butto开发者_StackOverflow社区n from scratch using this code:

UIButton *CustomButton = [UIButton buttonWithType:UIButtonTypeCustom];
[CustomButton setFrame:CGRectMake(20, 100, 280, 45)];
[CustomButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];

[CustomButton setTitle:@"Custom Button" forState:UIControlStateNormal];
[CustomButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[CustomButton setBackgroundImage:[UIImage imageNamed:@"button_blue.png"] forState:UIControlStateNormal];

[CustomButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[CustomButton setBackgroundImage:[UIImage imageNamed:@"button_orange.png"] forState:UIControlStateHighlighted];

[self.view addSubview:CustomButton];

There is no such white background peeping through.

I really would like to use the 'Round Rect' method of placing and skinning buttons because i'd like to see their positions in Interface builder. Is there a method i need to use or a property i need to amend to get rid of the white background of a 'Round Rect' button in code.

Here's an image showing the problem:

Problems applying custom coded images to a Round Rect Button in Interface Builder


I've found the answer!

Typical you spend hours hunting through manuals to find it just after posting!

Just change the button type to 'Custom' in Interface Builder and no extra code needed!

Problems applying custom coded images to a Round Rect Button in Interface Builder


Make the default colors for the button to be "clear color", the funny black and white diagonal thing.

0

精彩评论

暂无评论...
验证码 换一张
取 消