Swift 3 - 用于selectionIndicatorImage的UITabBarItem的全宽度 [英] Swift 3 - full width of UITabBarItem for selectionIndicatorImage

查看:73
本文介绍了Swift 3 - 用于selectionIndicatorImage的UITabBarItem的全宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有三个标签的UITabBar。现在我想分配或者说让一个标签的完整宽度填充到相关的selectionIndicatorImage,因为如果选择了一个标签,我现在有一个边框。就像左侧的标签显示在以下屏幕截图中:

i have a UITabBar with three tabs. Now I want to assign or lets say to fill the complete width of one tab to the related selectionIndicatorImage cause currently I got a border if a tab is selected. Like the tab on the left side shows in the following screenshot:

我使用新属性创建了UITabBar的子类:

I made a subclass of UITabBar with a new property:

var activeItemBackground:UIColor = UIColor.white {
    didSet {

        let numberOfItems = CGFloat((items!.count))

        let tabBarItemSize = CGSize(width: frame.width / numberOfItems,
                                    height: frame.height)

        selectionIndicatorImage = UIImage.imageWithColor(color: activeItemBackground,
                                     size: tabBarItemSize).resizableImage(withCapInsets: .zero)

        frame.size.width = frame.width + 4
        frame.origin.x = -2


    }
}

和UIImage-Extension以获得backgroundColo r和图像:

And the UIImage-Extension in order to have backgroundColor and an image:

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



<我读了很多关于这个问题的东西,但不幸的是我无法让它发挥作用。我的代码中缺少什么?

I read much stuff about this problem but unfortunately I can't get it to work. Is something missing in my code?

推荐答案

我认为你需要额外的几步......

I think you're taking a couple extra steps...

您正在计算标签栏项目的确切大小,并创建该大小的图像,因此您不需要 .resizableImage part。

You are calculating the exact size of the tab bar item, and creating an image of that size, so you shouldn't need the .resizableImage part.

而且,由于您设置的是精确尺寸,因此您也不需要调整标签栏的大小。

And, since you are setting to exact size, you also shouldn't need to resize the tab bar frame.

这似乎在我的测试中正常工作(使用 .imageWithColor func):

This appears to work fine in my testing (using your .imageWithColor func):

class MyTabBar: UITabBar {

    var activeItemBackground:UIColor = UIColor.white {
        didSet {

            let numberOfItems = CGFloat((items!.count))

            let tabBarItemSize = CGSize(width: frame.width / numberOfItems,
                                        height: frame.height)

            selectionIndicatorImage = UIImage.imageWithColor(color: activeItemBackground,
                                                             size: tabBarItemSize)

        }
    }

}

然后在第一个VC的viewDidLoad中:

Then in viewDidLoad of the first VC:

    if let tb = self.tabBarController?.tabBar as? MyTabBar {
        tb.activeItemBackground = UIColor.red
    }

这篇关于Swift 3 - 用于selectionIndicatorImage的UITabBarItem的全宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆