easythrees
easythrees

Reputation: 1660

iOS: adding images to a UIButton, then putting button in a scrollview, but only seeing one image?

I'm working on an iPad app and part of the UI is a scrollview that has buttons for its contents. I'm working on adding images to these buttons, but when I do so, I only ever get the image in one spot, when I should be seeing it on all the buttons. This is what I'm doing:

    float scrollCurrTop = 0;
    CGRect currProcedureButtonFrame = CGRectMake(0,
                                             scrollCurrTop,
                                             self.fProceduresView.frame.size.width,
                                             self.fLabelAndButtonHeight);
PatientIDButton* currProcedureButton = [PatientIDButton buttonWithType:UIButtonTypeCustom];
[currProcedureButton setFrame:currProcedureButtonFrame];    
[currProcedureButton.layer setBorderColor: [self.fPanelViewBorderColor CGColor]];
[currProcedureButton.layer setBorderWidth: self.fBorderWidth];
currProcedureButton.titleLabel.font = self.fLabelFont;

NSString* displayName = [grabbing name];
if (displayName == nil)
{
    displayName = currPlanName;
}

[currProcedureButton setTitle:displayName
                     forState:UIControlStateNormal];
[currProcedureButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
currProcedureButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
currProcedureButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

// is the plan approved?
if ([self isPlanApproved:currPlanName])
{
    // add the checkmark to this plan button
    CGRect currPlanButtonFrame = currProcedureButton.frame;
    float originX = currPlanButtonFrame.size.width - (currPlanButtonFrame.size.width/3.0f);
    float originY = currPlanButtonFrame.origin.y;
    float width = currPlanButtonFrame.size.width - originX;
    float height = currPlanButtonFrame.size.height;
    CGRect currPlanApprovalImageFrame = CGRectMake(originX, originY, width, height);
    UIImageView* currPlanApprovalImage = [[UIImageView alloc] initWithFrame:currPlanApprovalImageFrame];
    [currPlanApprovalImage setBackgroundColor:[UIColor colorWithRed:(63.0f/255.0f)
                                                              green:(179.0f/255.0f)
                                                               blue:(79.0f/255.0f)
                                                              alpha:1.0f]];
    [currPlanApprovalImage setImage:self.fCheckMarkIcon];
    [currProcedureButton addSubview:currPlanApprovalImage];
}

[self.fProceduresView addSubview:currProcedureButton];    
scrollCurrTop += self.fLabelAndButtonHeight;

Where 'fProceduresView' is the scrollview that houses the buttons. What am I doing wrong?

Upvotes: 0

Views: 46

Answers (1)

tt.Kilew
tt.Kilew

Reputation: 6084

It seems that you're misunderstanding the logic behing setting the frame of the imageview's those you're trying to add

CGRect currPlanButtonFrame = currProcedureButton.frame;
    float originX = currPlanButtonFrame.size.width - (currPlanButtonFrame.size.width/3.0f);
    float originY = currPlanButtonFrame.origin.y;
    float width = currPlanButtonFrame.size.width - originX;
    float height = currPlanButtonFrame.size.height;
    CGRect currPlanApprovalImageFrame = CGRectMake(originX, originY, width, height);
    UIImageView* currPlanApprovalImage = [[UIImageView alloc] initWithFrame:currPlanApprovalImageFrame];

You don't need to set originY to currPlanButtonFrame.origin.y; All subviews have relative coordinates to their's superviews.

So in your case originY should be 0

For example:

// Image is visible
Button frame [0, 0, 100, 100]
image in button [0, 0, 100, 100]  

// Button is visible, but image is not because it will be clipped by bounds of the button.
Button frame [100, 100, 100, 100]
image in button [100, 100, 100, 100]  

Also you will be able to "see" your images, if you set

currProcedureButton.clipsToBounds = NO

Upvotes: 1

Related Questions