美工設(shè)計(jì)的TabBar頂部分割線的顏色比iOS系統(tǒng)自帶的顏色要淺很多,所以需要修改系統(tǒng)tabBar的顏色.
網(wǎng)上搜索了一下,大部分做法是,調(diào)用tabBar的 setShadowImage: 和 setBackgroundImage: 方法
[self.tabBar setShadowImage:[UIImage qy_imageWithColor:[UIColor whiteColor]]];
[self.tabBar setBackgroundImage:[UIImage qy_imageWithColor:[UIColor clearColor]]];
但是這樣直接修改tabBar的backgroundImage可能會引入新的bug,導(dǎo)致導(dǎo)航欄顯示異常
導(dǎo)航欄顯示正常
導(dǎo)航欄顯示異常
下面介紹一種最簡單的方法:
使用XCode自帶的Debug View Hierarchy可以看到,tabBar上有一條細(xì)線
image.png
打印這個控件的信息可以看到
可以看出它個坐標(biāo)超出父控件0.5
所以就有了思路了:
在這個控件上面添加一條新的分割線,把那條線給遮住就行了
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -0.5, SCREEN_WIDTH, 0.5)];
view.backgroundColor = [UIColor redColor];
[[UITabBar appearance] insertSubview:view atIndex:0];
運(yùn)行效果:
image.png
同理,如果去掉tabBar自帶的毛玻璃效果,而使用純色,可以這樣做:
UIView *customBackgroundView = [[UIView alloc] initWithFrame:self.tabBar.bounds];
customBackgroundView.backgroundColor = [UIColor whiteColor];
[[UITabBar appearance] insertSubview:customBackgroundView atIndex:0];