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

Jam*_*man 14 iphone objective-c uibutton ios

我正在开发一个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];
Run Code Online (Sandbox Code Playgroud)

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

纽扣

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

inc*_*iko 17

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

像这样做:

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];
Run Code Online (Sandbox Code Playgroud)


iiF*_*man 5

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


Raj*_*han 5

默认情况下,按钮类型为System,将按钮的类型更改为Custom


修复代码:

[UIButton buttonWithType:UIButtonTypeCustom];


要解决的情节提要:

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

在此处输入图片说明