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

我尝试使用UITabBarItem.appearance(),但没有成功。

最佳答案

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

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

swift - 在Swift中将一行作为选择指示符添加到UITabbarItem-LMLPHP

关于swift - 在Swift中将一行作为选择指示符添加到UITabbarItem,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33667481/

10-09 18:31
查看更多