UIImageView и UIButton не выстраиваются - PullRequest
0 голосов
/ 19 сентября 2011

У меня есть 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];
} 

1 Ответ

1 голос
/ 19 сентября 2011

Каждое представление имеет свое происхождение для своих подпредставлений.

, поэтому установка 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];
...