UIImageView 和 UIButton 未对齐

发布于 2024-12-05 03:14:01 字数 1771 浏览 0 评论 0原文

我有一个 UIButton,里面有一个 UIImageView。由于某种原因,该按钮显示在图像的左侧。这是我的代码:

// Load resources
for (int x = 0; x < [self.gameOptions.categories count]; x++)
{
    Category* category = [self.gameOptions.categories objectAtIndex:x];
    UIButton* imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    NSMutableArray* imageArray = [NSMutableArray new];
    UIImageView* imageView = [[UIImageView alloc] init];

    for (int i = 0; i < [category.resources count]; i++)
    {
        Resource* resource = [category.resources objectAtIndex:i];

        [imageArray addObject:resource.targetImage];
    }

    imageView.animationDuration = [imageArray count] * 2;
    imageView.animationImages = imageArray;
    int count = [self.categoryButtons count];
    imageView.frame = CGRectMake((count) * 50 + (count) * 10, 0, 50, 50);
    [imageView startAnimating];

    imageButton.adjustsImageWhenHighlighted = NO;
    imageButton.tag = category.categoryId;
    [imageButton addTarget:self action:@selector(imageSelect:) forControlEvents:UIControlEventTouchUpInside];
    imageButton.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, imageView.frame.size.width, imageView.frame.size.height); 
    DLog(NSStringFromCGPoint(imageButton.layer.frame.origin));
    DLog(NSStringFromCGPoint(imageView.frame.origin));
    DLog(NSStringFromCGPoint(imageButton.frame.origin));
    DLog(NSStringFromCGPoint(imageView.layer.frame.origin));
    [imageButton addSubview:imageView];


    [categorySelection setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
    [categorySelection addSubview:imageButton];
    [self.categoryButtons addObject:imageButton];
    [imageArray release];
    [imageView release];
} 

I have a UIButton that has a UIImageView inside of it. For some reason the button is being displayed to the left of the images. Here is my code:

// Load resources
for (int x = 0; x < [self.gameOptions.categories count]; x++)
{
    Category* category = [self.gameOptions.categories objectAtIndex:x];
    UIButton* imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    NSMutableArray* imageArray = [NSMutableArray new];
    UIImageView* imageView = [[UIImageView alloc] init];

    for (int i = 0; i < [category.resources count]; i++)
    {
        Resource* resource = [category.resources objectAtIndex:i];

        [imageArray addObject:resource.targetImage];
    }

    imageView.animationDuration = [imageArray count] * 2;
    imageView.animationImages = imageArray;
    int count = [self.categoryButtons count];
    imageView.frame = CGRectMake((count) * 50 + (count) * 10, 0, 50, 50);
    [imageView startAnimating];

    imageButton.adjustsImageWhenHighlighted = NO;
    imageButton.tag = category.categoryId;
    [imageButton addTarget:self action:@selector(imageSelect:) forControlEvents:UIControlEventTouchUpInside];
    imageButton.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, imageView.frame.size.width, imageView.frame.size.height); 
    DLog(NSStringFromCGPoint(imageButton.layer.frame.origin));
    DLog(NSStringFromCGPoint(imageView.frame.origin));
    DLog(NSStringFromCGPoint(imageButton.frame.origin));
    DLog(NSStringFromCGPoint(imageView.layer.frame.origin));
    [imageButton addSubview:imageView];


    [categorySelection setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
    [categorySelection addSubview:imageButton];
    [self.categoryButtons addObject:imageButton];
    [imageArray release];
    [imageView release];
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拔了角的鹿 2024-12-12 03:14:01

每个视图都有自己的子视图来源。

因此,将 ImageView.frame.origin 设置为与 ButtonView.frame.origin 相同不会将它们放在与超级视图完全相同的位置。

所以你的 imageView 需要将原点设置为相对于按钮左上角的值。

imageButton.frame = CGRect(20.0, 20.0, 100.0, 100.0);
//then say you want the image to in the top left corner of the button
imageView.frame = CGRect(0.0, 0.0, 50.0, 50.0);
[imageButton addSubview:imageView];

Each view has its own origin for its subviews.

so setting the ImageView.frame.origin of the same as the ButtonView.frame.origin isn't going to put them in the exact same place in respect to the superview.

so your imageView needs the origin set to values relative to the top left corner of your button.

imageButton.frame = CGRect(20.0, 20.0, 100.0, 100.0);
//then say you want the image to in the top left corner of the button
imageView.frame = CGRect(0.0, 0.0, 50.0, 50.0);
[imageButton addSubview:imageView];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文