Put a line on a Uitabbaritem?

Asked

Viewed 47 times

0

How to place a line at the bottom of a Uitabbarbutton as it occurs in this image:

inserir a descrição da imagem aqui

However this line should only appear on the selected button.

  • 1

    Here explains very well... http://stackoverflow.com/questions/33667481/add-a-line-as-a-selection-indicator-to-a-uitabbaritem-in-swift

1 answer

1


You can do it with adding custom image, which will be created in your code, to selectionIndicatorImage in your Uitabbar object. For example, you can create extension for the Uiimage class like this:

extension UIImage {
    func createSelectionIndicator(color: UIColor, size: CGSize, lineWidth: CGFloat) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        color.setFill()
        UIRectFill(CGRectMake(0, size.height - lineWidth, size.width, lineWidth))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

And call it your first loaded Viewcontroller, something like this.

class FirstViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let tabBar = self.tabBarController!.tabBar
        tabBar.selectionIndicatorImage = UIImage().createSelectionIndicator(UIColor.blueColor(), size: CGSizeMake(tabBar.frame.width/CGFloat(tabBar.items!.count), tabBar.frame.height), lineWidth: 2.0)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

And the result will look like this.

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.