随着苹果发布会的开展,iOS 13 也随之而来,又来适配的 work 了,后续会持续更新,欢迎大家发现关于适配的问题,一起来解决它!
1、presentViewController的问题
原因:
主要是因为之前对UIViewController里面的一个属性,即modalPresentationStyle(该属性是控制器在模态视图时将要使用的样式)没有设置需要的类型。
在iOS13中modalPresentationStyle的默认改为UIModalPresentationAutomatic,而在之前默认是UIModalPresentationFullScreen。
解决方案:
只要将modalPresentationStyle设置成UIModalPresentationFullScreen即可解决该问题。
2、私有KVC
原因:
iOS不允许valueForKey、setValue: forKey获取和设置私有属性,需要使用其它方式修改
解决方案:
[textField setValue:[UIColor red] forKeyPath:@"_placeholderLabel.textColor"];
//替换为
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"输入"attributes:@{NSForegroundColorAttributeName: [UIColor red]}];
为了使用方便,可以给textField添加一个分类,给这个分类添加一个placeholdColor、placeholdFont属性,这样就不用每个对象都写一遍这段代码了。话不多说,直接上代码
@interface UITextField (Add)
/// 占位符的颜色
@property (nonatomic, strong) UIColor *placeholderColor;
/// 占位符的字号
@property (nonatomic, strong) UIFont *placeholdFont;
@end
#import "UITextField+Add.h"
static void *placeholderColorKey = &placeholderColorKey;
static void *placeholderFontKey = &placeholderFontKey;
@implementation UITextField (Add)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *oneSelector = NSStringFromSelector(@selector(setPlaceholder:));
NSString *twoSelector = NSStringFromSelector(@selector(setFont:));
NSArray *selectors = @[oneSelector, twoSelector];
[selectors enumerateObjectsUsingBlock:^(NSString *originalString, NSUInteger idx, BOOL * _Nonnull stop) {
///交换方法
NSString *swizzledSelString = [@"hmj_" stringByAppendingString:originalString];
Method originalMethod = class_getInstanceMethod(self, NSSelectorFromString(originalString));
Method swizzledMethod = class_getInstanceMethod(self, NSSelectorFromString(swizzledSelString));
BOOL didAddMethod = class_addMethod(self, NSSelectorFromString(originalString), method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(self, NSSelectorFromString(swizzledSelString), method_getImplementation(originalMethod), method_getTypeEncoding(swizzledMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}];
});
}
- (void)hmj_setPlaceholder:(NSString *)placeholder {
[self sinochem_setPlaceholder:placeholder];
[self setCustomePlaceholderColorAndFont];
}
- (void)hmj_setFont:(UIFont *)font {
[self sinochem_setFont:font];
[self setCustomePlaceholderColorAndFont];
}
// 这个函数是调整placeholder在placeholderLabel中绘制的颜色和字号
- (void)setCustomePlaceholderColorAndFont {
if (self.placeholder) {
NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName:self.placeholderColor, NSFontAttributeName:self.placeholdFont}];
self.attributedPlaceholder = placeholderString;
}
}
- (void)setPlaceholderColor:(UIColor *)placeholderColor {
objc_setAssociatedObject(self, &placeholderColorKey, placeholderColor, OBJC_ASSOCIATION_COPY);
}
- (UIColor *)placeholderColor {
UIColor *placeholderColor = objc_getAssociatedObject(self, &placeholderColorKey);
if (!placeholderColor) {
placeholderColor = self.textColor;
}
return placeholderColor;
}
- (void)setPlaceholdFont:(UIFont *)placeholdFont {
objc_setAssociatedObject(self, &placeholderFontKey, placeholdFont, OBJC_ASSOCIATION_COPY);
}
- (UIFont *)placeholdFont {
UIFont *placeholdFont = objc_getAssociatedObject(self, &placeholderFontKey);
if (!placeholdFont) {
placeholdFont = self.font;
}
return placeholdFont;
}
@end
注意:此方法设置的默认placeholderColor 和 placeholdFont 分别是self.textColor和self.font,如果有其他需求,可以自行修改。
此文章会持续更新,欢迎大家将发现的iOS 13 问题写到评论区,一起解决。
网友评论