2014-01-27 41 views
0

我正在研究iPad應用程序,UI的一部分是一個帶有按鈕的滾動視圖。我正在爲這些按鈕添加圖像,但是當我這樣做時,我只能在一個地方看到圖像,而我應該在所有按鈕上看到它。這是我在做什麼:iOS:將圖像添加到UIButton,然後將按鈕放在滾動視圖中,但只看到一個圖像?

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; 

其中'fProceduresView'是放置按鈕的滾動視圖。我究竟做錯了什麼?

+0

當調試'[currPlanApprovalImage setImage:self.fCheckMarkIcon];'在符合條件的'if'內部的行是否按預期的方式到達? – xicocaio

回答

1

看來你誤解了邏輯behing設置的ImageView的框架是那些你想添加

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]; 

你不需要originY設置爲currPlanButtonFrame.origin.y; 所有子視圖都具有相對於其超級視圖的座標。

所以你的情況originY應該是0

例如:

// 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] 

你也將能夠 「看到」 您的圖像,如果你設置

currProcedureButton.clipsToBounds = NO 
+0

就是這樣。謝謝你,這個解釋很有道理:)... – easythrees