这件事把我难住了.

是否有可能在iPhone的Cocoa中更改UIButton的背景色.

[random setBackgroundColor:[UIColor blueColor]];
[random.titleLabel setBackgroundColor:[UIColor blueColor]];

推荐答案

这可以通过制作副本以编程方式完成:

loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
[loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
loginButton.backgroundColor = [UIColor whiteColor];
loginButton.layer.borderColor = [UIColor blackColor].CGColor;
loginButton.layer.borderWidth = 0.5f;
loginButton.layer.cornerRadius = 10.0f;

编辑:当然,你得#import <QuartzCore/QuartzCore.h>美元

编辑:对于所有的新读者,你也应该考虑增加一些选项作为"另一种可能性".供你考虑.

As this is an old answer, I strongly recommend reading comments for troubleshooting

Objective-c相关问答推荐

跨平台 iPhone/Android 代码共享

如何用零填充左侧的整数?

当前时间是 HH:MM:SS am/pm 格式吗?

将 CSS 插入 UIWebView / WKWebView 中加载的 HTML

NSTimeInterval 格式

Objective-C 属性的默认属性是什么?

何时使用静态字符串与 #define

iOS 内存分配 - 一个应用程序可以使用多少内存?

未找到 Apple Mach-O 链接器警告目录

发出许多网络请求的 iOS 应用程序的最佳架构?

iOS 5 不允许将下载的数据存储在 Documents 目录中?

如何将@selector 作为参数传递?

Xcode 在 iOS 8 中的 Main() 中引发异常,并带有所有异常断点

如何在 Cocoa 中获取 NSArray 的第一个 x 元素?

隐藏 NSTableView 标头?

使用 respondsToSelector 时 suppress '...' is deprecated

cellForRowAtIndexPath 是如何工作的?

iOS 7 中的 UITextView 链接检测

performFetchWithCompletionHandler 永远不会被解雇

使用 cocoaLumberjack 将日志(log)文件存储在哪里