Set background color of active tab bar item in Swift

前端 未结 3 685
刺人心
刺人心 2020-12-07 23:13

I\'m hoping to accomplish this without the use of images, if at all possible. Is there a way to create the effect shown in the image programmatically without have to render

3条回答
  •  感情败类
    2020-12-08 00:04

    So here's what I ended up doing. It's a mix of using a 640x49 PNG that's the color of the blue "highlighted" background I need.

    In AppDelegate.swift:

    var selectedBG = UIImage(named:"tab-selected-full")?.resizableImageWithCapInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    UITabBar.appearance().selectionIndicatorImage = selectedBG
    

    And then in the first View Controller that gets loaded, I have:

    tabBarController?.tabBar.frame.size.width = self.view.frame.width+4
    tabBarController?.tabBar.frame.origin.x = -2
    

    The reason for the above two lines is that, by default, Apple has a 2px border between the left and right sides of the tab bar and the tab bar items.

    In the above I simply make the tab bar 4px wider, and then offset it so the border on the left falls just outside of the view, thus the border on the right will also fall outside of the view.

提交回复
热议问题