yon*_*nja 3 objective-c uibutton ios ios6
我想在我的视图中添加无边框UIButton.使用界面构建器,我通过从对象库中拖动Round Rect Button来完成此操作.然后,在属性检查器上,将"类型"更改为"自定义",并将标题保留为"按钮".界面构建器一切正常.但是,程序化方法并非如此.以编程方式,我就是这样做的:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(x, y, width, height)];
[button setTitle:@"Button" forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,不显示该按钮.也许我在这里遗漏了一些东西但是当我将类型更改UIButtonTypeRoundRect
为按钮时会显示.但是,我再次希望按钮无边框.
无论如何,我总是可以使用界面构建器.但是,我想了解为什么程序化方法不起作用.那么,有谁知道问题是什么?
小智 7
UIButtonTypeCustom
是真的,实际的自定义按钮类型.默认情况下,没有为其设置有意义的值.如果要显示它,则必须将其背景颜色和/或标题颜色设置为不透明的颜色:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(x, y, width, height)];
[button setTitle:@"Button" forState:UIControlStateNormal];
// Set visible values
[button setBackgroundColor:[UIColor greenColor]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[someSuperview addSubview:button];
Run Code Online (Sandbox Code Playgroud)