加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:45:40 所属栏目:百科 来源:网络整理
导读:我想在UITabbarItems底部使用粗线作为选择指标.由于应用程序必须适用于不同的手机尺寸,我无法使用图像作为选择指示器.这就是为什么我认为我必须使用Swift来做到这一点. (该行必须是页面宽度的1/3). 我试图使用UITabBarItem.appearance()但没有成功. 您可以通
我想在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.
    }
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读