设置导航栏返回键
let navigationitom = UIBarButtonItem(image: UIImage(named: "main_navi_left"), style: .plain, target: self, action: #selector(didClickBackButton))
self.navigationItem.leftBarButtonItem = navigationitom
暗黑模式的判断
UIUserInterfaceStyle userInterfaceStyle = [UITraitCollection currentTraitCollection].userInterfaceStyle;
if(userInterfaceStyle == UIUserInterfaceStyleLight){
}else{
}
- (void) traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
{
[super traitCollectionDidChange:previousTraitCollection];
UIUserInterfaceStyle userInterfaceStyle = previousTraitCollection.userInterfaceStyle;
if(userInterfaceStyle == UIUserInterfaceStyleLight){
}else{
}
}
屏蔽暗黑模式
plist文件设置
<key>UIUserInterfaceStyle</key>
<string>Light</string>
把字符串“#ff00ff00”转换为UIColor对象,前面两位代表透明度,后面分别代表RGB颜色
func colorFromHexString(_ hexString: String) -> UIColor? {
// 去掉字符串中的空格和换行符
let trimmedString = hexString.trimmingCharacters(in: .whitespacesAndNewlines)
// 判断字符串是否以 # 符号开头
guard trimmedString.hasPrefix("#") else {
return nil
}
// 截取字符串中的透明度、红、绿、蓝四种颜色的数值
let startIndex = trimmedString.index(trimmedString.startIndex, offsetBy: 1)
let endIndex = trimmedString.index(startIndex, offsetBy: 2)
let alphaIndex = trimmedString.index(startIndex, offsetBy: 2)
let redIndex = trimmedString.index(alphaIndex, offsetBy: 2)
let greenIndex = trimmedString.index(redIndex, offsetBy: 2)
let blueIndex = trimmedString.index(greenIndex, offsetBy: 2)
let alphaHex = String(trimmedString[startIndex..<endIndex])
let redHex = String(trimmedString[alphaIndex..<redIndex])
let greenHex = String(trimmedString[redIndex..<greenIndex])
let blueHex = String(trimmedString[greenIndex..<blueIndex])
// 将十六进制数值转换为 UIColor 对象
if let alpha = UInt8(alphaHex, radix: 16),
let red = UInt8(redHex, radix: 16),
let green = UInt8(greenHex, radix: 16),
let blue = UInt8(blueHex, radix: 16) {
return UIColor(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: CGFloat(alpha) / 255.0)
}
return nil
}
oc代码
+ (UIColor *)colorFromHexString:(NSString *)hexString {
// 去掉字符串中的空格和换行符
NSString *trimmedString = [hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// 判断字符串是否以 # 符号开头
if (![trimmedString hasPrefix:@"#"]) {
return nil;
}
// 截取字符串中的透明度、红、绿、蓝四种颜色的数值
NSRange alphaRange = NSMakeRange(1, 2);
NSRange redRange = NSMakeRange(3, 2);
NSRange greenRange = NSMakeRange(5, 2);
NSRange blueRange = NSMakeRange(7, 2);
NSString *alphaHex = [trimmedString substringWithRange:alphaRange];
NSString *redHex = [trimmedString substringWithRange:redRange];
NSString *greenHex = [trimmedString substringWithRange:greenRange];
NSString *blueHex = [trimmedString substringWithRange:blueRange];
// 将十六进制数值转换为 UIColor 对象
unsigned int alpha, red, green, blue;
NSScanner *scanner = [NSScanner scannerWithString:alphaHex];
[scanner scanHexInt:&alpha];
scanner = [NSScanner scannerWithString:redHex];
[scanner scanHexInt:&red];
scanner = [NSScanner scannerWithString:greenHex];
[scanner scanHexInt:&green];
scanner = [NSScanner scannerWithString:blueHex];
[scanner scanHexInt:&blue];
return [UIColor colorWithRed:(CGFloat)red / 255.0 green:(CGFloat)green / 255.0 blue:(CGFloat)blue / 255.0 alpha:(CGFloat)alpha / 255.0];
}
网友评论