ios - 重置文本时覆盖外观代理

标签 ios objective-c uitextfield uiappearance

我正在使用 UIAppearance 代理设置我的 UI。我将我的 UITextfields 设置为具有如下自定义字体:

[[UILabel appearanceWhenContainedIn:[RDTextField class], nil] setTextColor:[UIColor rds_purple]];
[[UILabel appearanceWhenContainedIn:[RDTextField class], nil] setFont:[UIFont rds_ralewayWithSize:20.0]];

这完美地工作,当我的文本字段出现在屏幕上时,它们具有正确的字体。但是,我使我的用户能够像这样对文本字段中的字符串进行洗牌:
- (IBAction)shuffleValuesButtonPressed:(id)sender {
    self.randomStringTextfield.text = [self randomString];
}

发生这种情况时,字体会从我的自定义字体变为默认的黑色字体。为什么会发生这种情况,让我的新字符串成为我在外观代理中设置的自定义字体的解决方案是什么?

最佳答案

根据 Apple 开发人员文档:

iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back.



因此,此解决方案通过删除所有内容并将其添加回来来工作,但可能有更好的方法:
- (IBAction)shuffleValuesButtonPressed:(id)sender {
    self.randomStringTextfield.text = [self randomString];
    [self refreshViews];
}

- (void)refreshViews {
    for (UIWindow *window in [UIApplication sharedApplication].windows) {
        for (UIView *view in window.subviews) {
            [view removeFromSuperview];
            [window addSubview:view];
        }
    }
}

关于ios - 重置文本时覆盖外观代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35139443/

相关文章:

ios - textFieldShouldBeginEditing 不会在 Swift 1.2 中触发正确的文本字段

ios - UITextField 编辑已开始在 View endEditing 上调用

iphone - 判断UITableViewController中的编辑操作是删除还是添加

iphone - iOS 7 上的 UITableView 部分索引间距

ios - 类型 'NSPredicate has no subscript members'

objective-c - 上传大视频 iOS 时延长后台任务超时时间

objective-c - 使用 ScriptingBridge 和 Objective-C 查找前端 Safari 窗口

objective-c - 是否有可以进行代码语法高亮显示的 Objective-C/C 库?

ios - 在 'editable' 类型的对象上找不到属性 'UITextField *'

ios - 从 xcode ios 中删除未使用的方法