微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将一行作为选择指示符添加到Swift中的UITabbarItem

我想在UITabbarItems底部使用粗线作为选择指标.由于应用程序必须适用于不同的手机尺寸,我无法使用图像作为选择指示器.这就是为什么我认为我必须使用Swift来做到这一点. (该行必须是页面宽度的1/3).

我试图使用UITabBarItem.appearance()但没有成功.

您可以通过将在代码中创建的自定义图像添加到UITabBar对象上的selectionIndicatorImage来实现.例如,你可以像这样为UIImage类创建扩展:
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
    }
}

并在你第一次加载的ViewController中调用它,如下所示:

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.
    }
}

在这种情况下,结果将是这样的:

原文地址:https://www.jb51.cc/swift/320043.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐