iphone - UIButton setTitleColor 和 setImage 不起作用

标签 iphone uitableview uibutton

我正在为我的 iPhone 项目使用 StoryBoard 功能。在 Storyboard中,我有一个 View Controller ,其中包含使用原型(prototype)单元的 TableView 。在原型(prototype)单元内部,我有一个 UIButton(圆角矩形)连接到子类 UIButton 类。 tableview从数组中获取数据,并将数据打印在cellForRowAtIndexPath中。

在 cellForRowAtIndexPath 内,我有条件地修改单元格的外观。这是我开始遇到问题的时候。我能够成功更改按钮的文本。但是,如果我尝试更改按钮的图像,请使用:

[cell.myButton setImage:[UIImage imageNamed:@"mynewimage.png"] forState:UIControlStateNormal];

图像发生变化,但文字消失。

如果我尝试更改按钮的文本颜色,请使用:

[cell.myButton setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.77] forState:UIControlStateNormal]

什么也没发生。颜色保持不变(除非我也尝试更改图像,在这种情况下它会完全消失)。

我感觉我在这里做了一些根本错误的事情——你有什么建议吗?

最佳答案

使用

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;   

更改图像并保留文本

使用

btn.titleLabel.textColor = yourcolor;

更改文本颜色

关于iphone - UIButton setTitleColor 和 setImage 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10479977/

相关文章:

iphone - 知道堆栈中位于我上方的 UIViewController 何时被弹出的最佳方法是什么?

ios - 如何在 willDisplayCell 方法中配置 UITableViewCell?

ios - TableView 复制图像?

iphone - 为 UILabel 文本添加下划线

ios - 滚动 UITableView 仅在 iPhone 5C 上导致异常和崩溃

ios - 带有 xam.Plugin.Media 的 iOS 10 上的相机问题

ios - 按 NSDate 对表格部分进行排序

iOS : subview of a button not showing up

ios - 从绑定(bind)到多个 UIButtons 的 GestureRecognizer 获取发件人信息

ios - 设置 UIButton 的 titleLabel 的字体会将 textColor 更改为默认字体?