gpt4 book ai didi

objective-c - 左右两侧带有图像的 UIButton

转载 作者:太空狗 更新时间:2023-10-30 03:50:06 32 4
gpt4 key购买 nike

我有一个中间有标题的 UIButton。我想在按钮的左侧和右侧显示图像(图标)。在左侧,很简单,我只是手动设置坐标。但在右侧,当显示旋转且按钮放大时,我需要沿“x”轴移动图像。

如何做到这一点?

最佳答案

假设您有一个名为 button1 的 UIButton,您可以这样做:

imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
imageView1.center = CGPointMake(25, button1.frame.size.height / 2);
imageView1.image = [UIImage imageNamed:@"ThumbsUp.png"];
[button1 addSubview:imageView1];
[imageView1 release];

imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
imageView2.center = CGPointMake(button1.frame.size.width - 25, button1.frame.size.height / 2);
imageView2.image = [UIImage imageNamed:@"ThumbsDown.png"];
[button1 addSubview:imageView2];
[imageView2 release];

这里我们向按钮添加两个 ImageView ,并使用它们的中心来定位它们。对于 imageView1,我们将其设置为距按钮左侧 25 像素。对于 imageView2,我们从按钮的宽度中减去 25。

现在我们必须设置旋转代码:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {

if (interfaceOrientation == UIDeviceOrientationPortrait || interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {
[button1 setFrame:CGRectMake(10, 100, 300, 50)];
} else {
[button1 setFrame:CGRectMake(40, 50, 400, 50)];
}
imageView2.center = CGPointMake(button1.frame.size.width - 25, button1.frame.size.height / 2);
}

这里我们根据纵向还是横向更改按钮的框架,最后我们设置 imageView2 的中心以适应不同的框架。我们不必为 imageView1 操心,因为它保持不变。

关于objective-c - 左右两侧带有图像的 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12426667/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com