从取消选择的选项卡UITabBarItem Swift中删除蒙版 [英] Remove mask from deselected tabs UITabBarItem Swift
问题描述
我正在尝试使用2 UITabBarItems
实现 UITabBarController
。我在故事板中添加了TabBarController。我几乎做到了,但我仍被阻止了两个重要问题:
I am trying to implement an UITabBarController
with 2 UITabBarItems
. I added in storyboard the TabBarController. I almost did it, but still I am blocked with 2 important issues:
1)以下是标签栏的外观:
1) Here is how tab bar should look:
请忽略橙色按钮,这不是tabItem。
所以我放了2个tabItems,我想保留两个标签的白色图像,即使其中一个被选中。
我用 tintColor
, barTintColor
检查了很多次,但没有成功。
Please ignore orange button, that is not a tabItem.
So I put 2 tabItems , and I want to keep white images for both tabs even if one of them is selected.
I checked a lot of times with tintColor
, barTintColor
and no success.
我还尝试在ViewController中设置tabBarItem:
Also I tried to set tabBarItem in ViewController:
override func awakeFromNib() {
super.awakeFromNib()
let imgHome = UIImage(named: "btnHome")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let imgProfile = UIImage(named: "btnProfile")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let imgSelectedTab = UIImage(named: "selectedTab_imgBackground")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
tabBarItem = UITabBarItem(title: nil, image: imgProfile, selectedImage: imgSelectedTab)
}
但没有成功。有没有想过这个问题?
but no success. Any thoughts at this issue ?
2)第二个问题是关于 selectedImage
UITabBarItem的属性
class。
图片的宽度
不适合标签。我在设备之间进行了更改,并且对于每个设备,所选图像都在另一个选项卡上,或者不适合当前选项卡。(我找到了一个解决方案:为每个设备提供相同的图像但宽度不同。但是肯定这是不是一个好的解决方案)
2) Second issue is about selectedImage
property of UITabBarItem
class.
The width
of image does not fit the tab. I changed between devices, and for every device the selected image is over the other tab, or does not fit the current tab.(I found a solution: to have the same image but with different width for every device. But for sure this is not a good solution)
任何一种帮忙会好的!
非常感谢
Any kind of help will be fine! Many thanks
推荐答案
以下是我如何管理这两个问题的完整示例:
https://github.com/AndreiBoariu/TabBarController
对于第一期,我在 UITabBarController $ c $中使用
for
循环解决了c> class:
For first issue, I solved using this for
loop in UITabBarController
class:
for item in tabBar.items as! [UITabBarItem] {
if let image = item.image {
item.image = image.imageWithColor(UIColor.whiteColor()).imageWithRenderingMode(.AlwaysOriginal)
}
}
这是 UIImage
<的扩展名/ p>
and here is the extension of UIImage
public extension UIImage {
func imageWithColor(tintColor: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
let context = UIGraphicsGetCurrentContext() as CGContextRef
CGContextTranslateCTM(context, 0, self.size.height)
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeNormal)
let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
CGContextClipToMask(context, rect, self.CGImage)
tintColor.setFill()
CGContextFillRect(context, rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
UIGraphicsEndImageContext()
return newImage
}
}
For第二个问题,检查来自github的代码;)
For second issue, check code from github ;)
这篇关于从取消选择的选项卡UITabBarItem Swift中删除蒙版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!