带 2 个按钮的分段控件
我正在尝试创建一个带有 2 个圆形矩形按钮的分段控件,但不知道如何为突出显示按钮设置“forState”。
其他人说要在 IB 中设置背景图像并以编程方式修改图像
尝试过:
[buttonName setImage:[UIImage imageNamed:@"UnselectedImage.png"] forState:UIControlStateNormal];
[buttonName setImage:[UIImage imageNamed:@"SelectedImage.png"] forState:UIControlStateHighlighted];
问题是背景图像在我单击它后不再保留,有任何线索吗?
I'm trying to create a segmented control with 2 Round rect buttons but don't know how to set the 'forState' for Highlighted button.
Others said to set the background image in IB and modify the Image programmatically
Tried:
[buttonName setImage:[UIImage imageNamed:@"UnselectedImage.png"] forState:UIControlStateNormal];
[buttonName setImage:[UIImage imageNamed:@"SelectedImage.png"] forState:UIControlStateHighlighted];
The problem is the background image doesn't remanins after I've clicked on it, any clue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试以下代码:
1 个图像:
2 个图像:
You could try the following code:
1 Image:
2 Images:
仅当用户触摸按钮时才使用“突出显示”状态。一旦用户从按钮上松开手指,状态就会恢复正常。
您可以尝试使用“选定”状态(未经测试但值得尝试,因为它看起来很合乎逻辑)。如果它不起作用,您可能需要通过代码自行更改按钮的“选定”状态(在按钮操作或segmentedControl委托方法中)
The "highlighted" state is used only while the user touches the button. As soon as the user release his/her finger from the button, the states goes back to normal.
You may try using the "Selected" state instead (not tested but worth trying as it seems quite logical). If it doesn't work, you may need to change the "selected" state of your buttons yourself by code (in the button action or the segmentedControl delegate methods)
您需要为
UIControlStateSelected
设置所选图像,而不是UIControlStateHighlighted
You need to set the selected image for
UIControlStateSelected
, rather thanUIControlStateHighlighted
UIButton 有点像瞬时开关。它没有“现在很热”的状态。 “突出显示”实际上是在被点击时,然后就脱离了该状态。
看来您可能需要一些外部变量来跟踪按钮的状态,并且可能还需要管理图像的旋转。
我喜欢那个形象。一旦运行起来,效果会很好。
A UIButton is kind of like a momentary switch. It doesn't have a "hot now" state. "Highlighted" is literally just while it's being tapped, and then it's out of that state.
It seems like you maybe need some external variable to track the state of your buttons, and maybe to manage the twiddling of the images, too.
I like that image. That's going to be a nice effect, once you get it running.