Reputation: 2941
I have a pretty strange problem. In one view I have three buttons, each with an image and a title. First button is OK, second only shows an image, and the third shows the right image, but the title from Button 2
. Like below:
------------
I Image 1 I
I Title 1 I
------------
------------
I Image 2 I
I I
------------
------------
I Image 3 I
I Title 2 I
------------
I can't find what is causing this.
I have a UIButton subclass with a custom init method that looks like this:
- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)image andTitle:(NSString *)title
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
UIImageView *icon = [[UIImageView alloc] initWithImage:image];
icon.backgroundColor = [UIColor clearColor];
CGRect iconFrame = icon.frame;
iconFrame.origin.x = frame.size.width / 2 - iconFrame.size.width / 2;
iconFrame.origin.y = 5;
iconFrame.size.height = frame.size.height / 2.5;
icon.frame = iconFrame;
[self addSubview:icon];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.font = [UIFont fontWithName:@"TisaMobiPro" size:14];
[titleLabel setText:title];
titleLabel.textColor = [UIColor colorWithRed:1.000 green:1.000 blue:1.000 alpha:0.5];
titleLabel.backgroundColor = [UIColor clearColor];
CGRect titleFrame = titleLabel.frame;
titleFrame.origin.y = icon.frame.origin.y + icon.frame.size.height;
titleFrame.size.height = frame.size.height / 2;
titleLabel.frame = titleFrame;
[self addSubview:titleLabel];
}
return self;
}
I add the buttons like this:
int numberOfButtons = 3;
CGRect mentionRect = CGRectMake(0, 10,self.menuContainer.frame.size.width / numberOfButtons, self.menuContainer.frame.size.height - 10);
self.mentionButton = [[CustomBarButton alloc] initWithFrame:mentionRect withImage:[UIImage imageNamed:@"Images/Mentions.png"] andTitle:@"Mention"];
[self.menuContainer addSubview:self.mentionButton];
CGRect hashRect = CGRectMake(mentionRect.origin.x + mentionRect.size.width, 10,self.menuContainer.frame.size.width / numberOfButtons, self.menuContainer.frame.size.height - 10);
self.hashtagButton = [[CustomBarButton alloc] initWithFrame:hashRect withImage:[UIImage imageNamed:@"Images/HashTagIcon.png"] andTitle:@"Hashtag"];
[self.menuContainer addSubview:self.hashtagButton];
CGRect photoRect = CGRectMake(hashRect.origin.x + hashRect.size.width, 10, self.menuContainer.frame.size.width / numberOfButtons, self.menuContainer.frame.size.height - 10);
self.photoButton = [[CustomBarButton alloc] initWithFrame:photoRect withImage:[UIImage imageNamed:@"Images/CameraIcon.png"] andTitle:@"Photo"];
[self.menuContainer addSubview:self.photoButton];
Can anyone find what is causing this, or have any idea on what I can do to solve it?
Upvotes: 1
Views: 79
Reputation: 23278
Check the frame setting in UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
. You should not set frame as frame
which is an input param in init method. Try to set a static frame and then modify according to your requirement to put it where ever you need it in that view. Basically your origin.x
value is going wrong and that is why you are getting weird output.
Upvotes: 1