2011-09-19 7 views
0

UIImageView가있는 UIButton이 있습니다. 어떤 이유로 버튼이 이미지의 왼쪽에 표시됩니다. 여기 내 코드는 다음과 같습니다.UIImageView와 UIButton이 일렬로 정렬되어 있지 않습니다.

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

각보기의 하위보기에 대한 자체 원점이 있습니다.

그래서 ButtonView.frame.origin과 같은 ImageView.frame.origin을 설정하면 슈퍼 뷰와 똑같은 위치에 표시되지 않습니다.

그래서 이미지 뷰의 원점은 버튼의 왼쪽 상단과 관련된 값으로 설정해야합니다.

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

아! 나는 그것이 상대적 위치라는 것을 몰랐다. 알아 둘만한. – user472292

관련 문제