1.適配UITabBar
- (void)setupTabBar
{
UIColor *backgroundColor = [UIColor whiteColor];
UIColor *shadowColor = UIColorFromHEX(0xE5E5E5);
if (@available(iOS 13.0, *)) {
UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init];
appearance.backgroundColor = backgroundColor;
appearance.shadowColor = shadowColor;
appearance.backgroundEffect = nil;
appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:10], NSForegroundColorAttributeName:kColor999999};
appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:10], NSForegroundColorAttributeName:kColorTheme};
self.tabBar.standardAppearance = appearance;
if (@available(iOS 15.0, *)) {
self.tabBar.scrollEdgeAppearance = appearance;
}
} else {
UIImage *shadowImage = [Utils imageWithColor:shadowColor size:CGSizeMake(SCREEN_WIDTH, 0.5f)];
UIImage *backgroundImage = [Utils imageWithColor:backgroundColor size:CGSizeMake(SCREEN_WIDTH, kTabbarHeight)];
[self.tabBar setShadowImage:shadowImage];
[self.tabBar setBackgroundImage:backgroundImage];
}
}
2.適配UINavigationBar
- (void)setupNavigateionBar
{
UIColor *backgroundColor = [UIColor whiteColor];
UIColor *shadowColor = [UIColor whiteColor];
NSDictionary *titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor blackColor],
NSFontAttributeName: [UIFont systemFontOfSize:18 weight:UIFontWeightBold]};
if (@available(iOS 13.0, *)) {
UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
appearance.backgroundColor = backgroundColor;
appearance.shadowColor = shadowColor;
appearance.titleTextAttributes = titleTextAttributes;
self.navigationBar.standardAppearance = appearance;
if (@available(iOS 15.0, *)) {
self.navigationBar.scrollEdgeAppearance = appearance;
}
}else{
self.navigationBar.barTintColor = backgroundColor;
self.navigationBar.titleTextAttributes = titleTextAttributes;
[self.navigationBar setShadowImage:[Utils imageWithColor:shadowColor size:CGSizeMake(SCREEN_WIDTH, 0.5f)]];
[self.navigationBar setBackgroundImage:[Utils imageWithColor:backgroundColor size:CGSizeMake(SCREEN_WIDTH, kStatusBarAndNavigationBarHeight)] forBarMetrics:UIBarMetricsDefault];
}
// 不透明
self.navigationBar.translucent = NO;
// navigation控件顏色
self.navigationBar.tintColor = [UIColor blackColor];
}
其中用到的宏和工具方法
// 16進制顏色
#define UIColorFromHEX(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
// 設備寬度
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
// 判斷是否異性全面屏
#define kIsFullScreen ({\
BOOL isBangsScreen = NO; \
if (@available(iOS 11.0, *)) { \
UIWindow *window = [[UIApplication sharedApplication].windows firstObject]; \
isBangsScreen = window.safeAreaInsets.bottom > 0; \
} \
isBangsScreen; \
})
// Tabbar height.
#define kTabbarHeight (kIsFullScreen ? (49.f+34.f) : 49.f)
// Status bar & navigation bar height.
#define kStatusBarAndNavigationBarHeight (kIsFullScreen ? 88.f : 64.f)
// 根據(jù)顏色生成圖片
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[color setFill];
UIRectFill(rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者