ios - How to change color for tab bar non selected icon in swift? -
how change color tab bar non selected icon , text? found answer (how change inactive icon/text color on tab bar?), can't implement swift.
the below sets defaults uitabbaritem's, can add appdelegate
. change text color.
uitabbaritem.appearance().settitletextattributes({nsforegroundcolorattributename: uicolor.blackcolor()}, forstate:.selected) uitabbaritem.appearance().settitletextattributes({nsforegroundcolorattributename: uicolor.whitecolor()}, forstate:.normal)
for changing icon' color can either set image given state image have color.
self.tabbaritem.selectedimage = [[uiimage imagenamed:@"selectedimage"] imagewithrenderingmode:uiimagerenderingmodealwaysoriginal]; self.tabbaritem.image = [[uiimage imagenamed:@"notselectedimage"] imagewithrenderingmode:uiimagerenderingmodealwaysoriginal];
or can way :
add extension uiimage
class (from this answer) :
extension uiimage { func imagewithcolor(color1: uicolor) -> uiimage { uigraphicsbeginimagecontextwithoptions(self.size, false, self.scale) let context = uigraphicsgetcurrentcontext() cgcontextref cgcontexttranslatectm(context, 0, self.size.height) cgcontextscalectm(context, 1.0, -1.0); cgcontextsetblendmode(context, kcgblendmodenormal) let rect = cgrectmake(0, 0, self.size.width, self.size.height) cgrect cgcontextcliptomask(context, rect, self.cgimage) color1.setfill() cgcontextfillrect(context, rect) let newimage = uigraphicsgetimagefromcurrentimagecontext() uiimage uigraphicsendimagecontext() return newimage } }
and in viewdidload
:
for item in self.tabbar.items [uitabbaritem] { if let image = item.image { item.image = image.imagewithcolor(uicolor.blackcolor()).imagewithrenderingmode(.alwaysoriginal) } }
Comments
Post a Comment