ios - How can I change the color of the UITabBar top border by creating a UIImage programmatically? -
from understanding, way change color of top border set background image (320x49, pixel line @ top). seems me way (please correct me if i'm wrong).
is there way without using image file? example, helped me change navigationbar bottom border creating uiimage code:
uinavigationbar.appearance().shadowimage = uiimage.colorfornavbar(uicolor.redcolor()) extension uiimage { class func colorfornavbar(color: uicolor) -> uiimage { let rect = cgrectmake(0.0, 0.0, 1.0, 1.0) uigraphicsbeginimagecontext(rect.size) let context = uigraphicsgetcurrentcontext() cgcontextsetfillcolorwithcolor(context, color.cgcolor) cgcontextfillrect(context, rect) let image = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() return image } }
this solution works well; changes color of bottom border.
i tried apply tabbar, nothing changes @ all.
uitabbar.appearance().shadowimage = uiimage.colorfornavbar(.redcolor())
you've pretty answered own question. can same thing uitabbar
did uinavigationbar
. if want change shadow image (i.e. "top border"), have change background image. straight apple:
the custom shadow image tab bar. attribute ignored if tab bar not have custom background image. set attribute programmatically, use shadowimage property.
in own question seem aware of this:
the way change color of top border set background image (320x49, pixel line @ top)
except it's not background image has line @ top. have set background image anything, can set shadow image preference.
if open simple "tabbed application" template within xcode, you'll find adding these 2 lines of codes (and uiimage
extension code) indeed work:
// white background red border on top uitabbar.appearance().backgroundimage = uiimage.colorfornavbar(.whitecolor()) uitabbar.appearance().shadowimage = uiimage.colorfornavbar(.redcolor())