iOS : How to add Underline in UITabBarItem

后端 未结 5 1510
情书的邮戳
情书的邮戳 2021-02-09 14:36

I am working in a application where i need to add underline in UITabbarItem.

so i would like to add underline under the selected UITabbarItem i

5条回答
  •  名媛妹妹
    2021-02-09 14:53

    In the AppDelegate didFininshLaunch Method Add Global Style for UITabbar

    UITabBar.appearance().selectionIndicatorImage = UIImage.getImageWithColorPosition(UIColor.whiteColor(), size: CGSizeMake(kScreenWidth/4, 49), lineSize: CGSizeMake(kScreenWidth/4, 2))
    
    class func getImageWithColorPosition(color: UIColor, size: CGSize, lineSize: CGSize) -> UIImage {
        let rect = CGRectMake(0, 0, size.width, size.height)
        let rectLine = CGRectMake(0, size.height-lineSize.height, lineSize.width, lineSize.height)
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        UIColor.clearColor().setFill()
        UIRectFill(rect)
        color.setFill()
        UIRectFill(rectLine)
        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    

    EDIT Swift 3

    func getImageWithColorPosition(color: UIColor, size: CGSize, lineSize: CGSize) -> UIImage {
    
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        let rectLine = CGRect(x: 0, y: size.height-lineSize.height, width: lineSize.width, height: lineSize.height)
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        UIColor.clear.setFill()
        UIRectFill(rect)
        color.setFill()
        UIRectFill(rectLine)
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }
    

提交回复
热议问题