Objective c UIImageView和UIButton未对齐

Objective c UIImageView和UIButton未对齐,objective-c,xcode,uiimageview,uibutton,Objective C,Xcode,Uiimageview,Uibutton,我有一个UIButton,里面有一个UIImageView。由于某些原因,按钮显示在图像的左侧。这是我的密码: // Load resources for (int x = 0; x < [self.gameOptions.categories count]; x++) { Category* category = [self.gameOptions.categories objectAtIndex:x]; UIButton* imageButton = [UIButton

我有一个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];
} 
//加载资源
对于(int x=0;x<[self.gameOptions.categories count];x++)
{
Category*Category=[self.gameOptions.categories objectAtIndex:x];
UIButton*imageButton=[UIButton按钮类型:UIButtonTypeCustom];
NSMUTABLEARRY*imageArray=[NSMUTABLEARRY new];
UIImageView*imageView=[[UIImageView alloc]init];
对于(int i=0;i<[category.resources count];i++)
{
Resource*Resource=[category.resources objectAtIndex:i];
[imageArray addObject:resource.targetImage];
}
imageView.animationDuration=[imageArray计数]*2;
imageView.animationImages=imageArray;
int count=[self.categoryButtons count];
imageView.frame=CGRectMake((计数)*50+(计数)*10,0,50,50);
[imageView startAnimating];
imageButton.adjustsImageWhenHighlighted=否;
imageButton.tag=category.categoryId;
[imageButton添加目标:自我操作:@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));
[图像按钮添加子视图:图像视图];
[类别选择集contentsize:CGSizeMake(imageView.frame.size.width,imageView.frame.size.height)];
[类别选择添加子视图:图像按钮];
[self.categoryButtons addObject:imageButton];
[图像阵列发布];
[图像视图发布];
} 

每个视图都有自己的子视图原点

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

因此,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];

啊!!我不知道这是一个相对的位置。很高兴知道。