ios8 - Swift TabBar Item color and background color -
i'm trying set icon color , background color of individual item on tabbar within tabbarcontroller.
i have 5 icons on tabbar, 4 of i've set color for, it's last icon i'm struggling with. i've attached link below of image show i'm trying achieve.
the specific icon (the 1 in middle of image) camera feed, icon white , background red. code far such - on tabbarcontroller:
var imagenames = ["feedicon", "exploreicon", "cameraicon", "activityicon", "myprofileicon"] let tabitems = tabbar.items as! [uitabbaritem] (index, value) in enumerate(tabitems) { var imagename = imagenames[index] value.image = uiimage(named: imagename) value.imageinsets = uiedgeinsetsmake(5.0, 0, -5.0, 0) } //for below i've used extension imagewithcolor set color of icons tabitems in self.tabbar.items as! [uitabbaritem] { if let image = tabitems.image { tabitems.image = image.imagewithcolor(uicolor(red: 57.0/255.0, green: 57.0/255.0, blue: 57.0/255.0, alpha: 1.0)).imagewithrenderingmode(.alwaysoriginal) } }
my extension:
extension uiimage { func imagewithcolor(tintcolor: 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) tintcolor.setfill() cgcontextfillrect(context, rect) let newimage = uigraphicsgetimagefromcurrentimagecontext() uiimage uigraphicsendimagecontext() return newimage } }
might bit late create "camera item" think solution create uibutton , set center of button center of uitabbar. can see how in post. after can set image button background color.
update:
i created sample project written swift can see how add button on tabbar.
the main issue subclass uitabbarcontroller , add button class.
class tabbarviewcontroller: uitabbarcontroller { override func viewdidload() { super.viewdidload() } override func didreceivememorywarning() { super.didreceivememorywarning() } override func viewdidappear(animated: bool) { // creates image of button let imagecamerabutton: uiimage! = uiimage(named: "cameraicon") // creates button let camerabutton = uibutton(type: .custom) // sets width , height button camerabutton.frame = cgrectmake(0.0, 0.0, imagecamerabutton.size.width, imagecamerabutton.size.height); // sets image button camerabutton.setbackgroundimage(imagecamerabutton, forstate: .normal) // sets center of button center of tabbar camerabutton.center = self.tabbar.center // sets action button camerabutton.addtarget(self, action: "dosomething", forcontrolevents: .touchupinside) // adds button view self.view.addsubview(camerabutton) } func dosomething() { print("action of camera button") } }
Comments
Post a Comment