我正在使用 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/