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 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每个视图都有自己的子视图来源。
因此,将 ImageView.frame.origin 设置为与 ButtonView.frame.origin 相同不会将它们放在与超级视图完全相同的位置。
所以你的 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.