我创建了两个相邻的按钮来模仿分段控件。我这样做是为了定制UIKit允许的外观。我决定使用所选属性按下按钮。我有两个图像,每个状态正常并选择一个。
问题在于,当我选择一个按钮时,由于高光状态,按钮会突出显示并变暗。我决定将所选图像用于高亮状态,但它闪烁,任何想法或建议。
- (void)leftSegmentPressed:(id)sender
{
if ([sender isSelected]) {
[sender setSelected:NO];
}
else {
[sender setSelected:YES];
}
}
答案 0 :(得分:1)
对于“已选择”按钮,请将其禁用并手动切换状态图像。
- (void) viewDidLoad
{
[rightSegmentButton setImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
[rightSegmentButton setImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateDisabled];
[leftSegmentButton setImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
[leftSegmentButton setImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateDisabled];
}
- (void)leftSegmentPressed:(id)sender
{
sender.enabled = NO;
rightSegmentButton.enabled = YES;
}
- (void)rightSegmentPressed:(id)sender
{
sender.enabled = NO;
leftSegmentButton.enabled = YES;
}
答案 1 :(得分:1)
检查您提供的图片是否在Bundle
中,或检查您提供的图片名称是否在lower case
。然后像
[button1 setImage:[UIImage imageNamed:@"normal1.png"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:@"selected1.png"] forState:UIControlStateSelected];
[button2 setImage:[UIImage imageNamed:@"normal2.png"] forState:UIControlStateNormal];
[button2 setImage:[UIImage imageNamed:@"selected2.png"] forState:UIControlStateSelected];
button1.tag = 1;
button2.tag = 2;
[button1 addTarget:self action:@selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]
[button2 addTarget:self action:@selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]
按钮事件方法中的
-(void)buttonSelected:(id)sender {
if([sender tag] == 1) {
button1.selected = YES;
button2.selected = NO;
} else {
button1.selected = NO;
button2.selected = YES;
}
}
答案 2 :(得分:0)
[button setAdjustsImageWhenHighlighted:NO];
这样可以防止闪烁。
答案 3 :(得分:0)
UIButton *yourButton1 = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
yourButton1.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
[yourButton1 setTitle:@"Left" forState:UIControlStateNormal];
yourButton.backgroundColor = [UIColor clearColor];
yourButton1.tag = 1;
[yourButton1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:@"yourNormalImage.png"];// set normal image
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[yourButton1 setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:@"yourSelectedImage.png"];// set selected image
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[yourButton1 setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[yourButton1 addTarget:self action:@selector(leftSegmentPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourButton1];
为另一个第二个按钮做同样的事。
并在动作方法中设置波纹管代码..
- (void)leftSegmentPressed:(id)sender
{
UIButton *btnTemp = (UIBUtton *)sender;
if (btnTemp.tag == 1) {
[yourButton1 setSelected:YES];
[yourButton2 setSelected:NO];
}
else {
[yourButton1 setSelected:NO];
[yourButton2 setSelected:YES];
}
}