gpt4 book ai didi

iOS:将图像添加到 UIButton,然后将按钮放入 ScrollView ,但只能看到一张图像?

转载 作者:行者123 更新时间:2023-11-29 03:18:36 25 4
gpt4 key购买 nike

我正在开发一个 iPad 应用程序,UI 的一部分是一个 ScrollView ,其中包含其内容的按钮。我正在努力向这些按钮添加图像,但是当我这样做时,我只能在一个位置获得图像,而我应该在所有按钮上看到它。这就是我正在做的事情:

    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”是包含按钮的 ScrollView 。我做错了什么?

最佳答案

看来您误解了设置 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;所有 subview 都有相对于它们的父 View 的坐标。

所以在你的情况下 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

关于iOS:将图像添加到 UIButton,然后将按钮放入 ScrollView ,但只能看到一张图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21393003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com