ios - Swift: How to remove border from segmented control -
how remove outside border of segmented control in xcode? i've set divider image wanted follow mock of app need have segmented control without outer border.
you can remove both borders , dividers using below function. create uisegmentedcontrol extension:
for swift 2.2:
extension uisegmentedcontrol { func removeborders() { setbackgroundimage(imagewithcolor(backgroundcolor!), forstate: .normal, barmetrics: .default) setbackgroundimage(imagewithcolor(tintcolor!), forstate: .selected, barmetrics: .default) setdividerimage(imagewithcolor(uicolor.clearcolor()), forleftsegmentstate: .normal, rightsegmentstate: .normal, barmetrics: .default) } // create 1x1 image color private func imagewithcolor(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 } }
for swift 3:
extension uisegmentedcontrol { func removeborders() { setbackgroundimage(imagewithcolor(color: backgroundcolor!), for: .normal, barmetrics: .default) setbackgroundimage(imagewithcolor(color: tintcolor!), for: .selected, barmetrics: .default) setdividerimage(imagewithcolor(color: uicolor.clear), forleftsegmentstate: .normal, rightsegmentstate: .normal, barmetrics: .default) } // create 1x1 image color private func imagewithcolor(color: uicolor) -> uiimage { let rect = cgrect(x: 0.0, y: 0.0, width: 1.0, height: 1.0) uigraphicsbeginimagecontext(rect.size) let context = uigraphicsgetcurrentcontext() context!.setfillcolor(color.cgcolor); context!.fill(rect); let image = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return image! } }
call above function.
segmentedcontrol.removeborders()
reference: remove uisegmentedcontrol separators completely. (iphone)
thanks https://stackoverflow.com/users/3921490/amagain swift 3 version.
Comments
Post a Comment