iOS应用程序中自定义UIButton旁边显示“蓝框”

时间:2022-06-24 05:22:33

I'm developing an iPhone app that acts as a remote for switching lightbulbs on and off, and I'm using UIButtons to do this:

我正在开发一个iPhone应用程序,它可以作为打开和关闭灯泡的遥控器,我正在使用UIButtons来做到这一点:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];

[button setBackgroundImage:bulb_on forState:UIControlStateSelected];
[button setBackgroundImage:bulb_off forState:UIControlStateNormal];

button.frame = CGRectMake(SPACING_LEFT + (BUTTON_SPACING * buttonNum) % (NUMBER_OF_HORIZONTAL_BUTTONS * BUTTON_SPACING), SPACING_TOP + y_padding, BUTTON_SIZE_X, BUTTON_SIZE_Y);

[self.scrollView addSubview:button];

Everything works fine, except a little, but still annoying detail:

一切都很好,除了一点点,但仍然令人讨厌的细节:

iOS应用程序中自定义UIButton旁边显示“蓝框”

As you can see, there is some kind of blue "box" or shadow in the top left corner of the selected button. The button in normal state has no such thing. What can this come from, and how to remove it?

如您所见,所选按钮的左上角有一些蓝色的“盒子”或阴影。正常状态下的按钮没有这样的东西。它可以来自什么,以及如何删除它?

3 个解决方案

#1


16  

I think it s because you created a UIButtonTypeRoundedRect not a buttonWithType:UIButtonTypeCustom

我认为这是因为你创建了一个UIButtonTypeRoundedRect而不是一个buttonWithType:UIButtonTypeCustom

Do it like this:

像这样做:

UIButton *button = [[UIButton alloc]initWithFrame: CGRectMake(SPACING_LEFT + (BUTTON_SPACING * buttonNum) % (NUMBER_OF_HORIZONTAL_BUTTONS * BUTTON_SPACING), SPACING_TOP + y_padding, BUTTON_SIZE_X, BUTTON_SIZE_Y)];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];

[button setBackgroundImage:bulb_on forState:UIControlStateSelected];
[button setBackgroundImage:bulb_off forState:UIControlStateNormal];

[self.scrollView addSubview:button];

#2


5  

Try this in programmatically [UIButton buttonWithType:UIButtonTypeCustom];

以编程方式尝试此操作[UIButton buttonWithType:UIButtonTypeCustom];

#3


4  

By default button type is System, Change type of your button to Custom.

默认情况下,按钮类型为系统,将按钮的类型更改为自定义。


Code to fix:

要修复的代码:

[UIButton buttonWithType:UIButtonTypeCustom];

[UIButton buttonWithType:UIButtonTypeCustom];


Storyboard to fix:

要修复的故事板:

Refer screenshot to fix in stroryboard.

请参阅屏幕截图以修复stroryboard。

iOS应用程序中自定义UIButton旁边显示“蓝框”

#1


16  

I think it s because you created a UIButtonTypeRoundedRect not a buttonWithType:UIButtonTypeCustom

我认为这是因为你创建了一个UIButtonTypeRoundedRect而不是一个buttonWithType:UIButtonTypeCustom

Do it like this:

像这样做:

UIButton *button = [[UIButton alloc]initWithFrame: CGRectMake(SPACING_LEFT + (BUTTON_SPACING * buttonNum) % (NUMBER_OF_HORIZONTAL_BUTTONS * BUTTON_SPACING), SPACING_TOP + y_padding, BUTTON_SIZE_X, BUTTON_SIZE_Y)];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];

[button setBackgroundImage:bulb_on forState:UIControlStateSelected];
[button setBackgroundImage:bulb_off forState:UIControlStateNormal];

[self.scrollView addSubview:button];

#2


5  

Try this in programmatically [UIButton buttonWithType:UIButtonTypeCustom];

以编程方式尝试此操作[UIButton buttonWithType:UIButtonTypeCustom];

#3


4  

By default button type is System, Change type of your button to Custom.

默认情况下,按钮类型为系统,将按钮的类型更改为自定义。


Code to fix:

要修复的代码:

[UIButton buttonWithType:UIButtonTypeCustom];

[UIButton buttonWithType:UIButtonTypeCustom];


Storyboard to fix:

要修复的故事板:

Refer screenshot to fix in stroryboard.

请参阅屏幕截图以修复stroryboard。

iOS应用程序中自定义UIButton旁边显示“蓝框”