- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想为每个选项卡更改“Tintcolor”的问题。但是下面的代码根本不起作用。
我添加了按钮图像并想使用“UIEdgeInsetsMake”调整它的大小。但是每当我触摸按钮时,按钮都会奇怪地调整大小。我不知道为什么。
我正在使用 Swift 3。
class MainView: UITabBarController {
var TabFirst = UITabBarItem()
var TabSecond = UITabBarItem()
var TabThird = UITabBarItem()
var TabForth = UITabBarItem()
var TabFifth = UITabBarItem()
override func viewDidLoad() {
super.viewDidLoad()
tabBar.barTintColor = UIColor.white
TabFirst = self.tabBar.items![0]
TabFirst.image = UIImage(named: "btn_1-1")!//.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
TabFirst.imageInsets = UIEdgeInsetsMake(12, 10, 11, 11)
tabBar.items?[0].title = "length"
TabSecond = self.tabBar.items![1]
TabSecond.image = UIImage(named: "btn_2-1")!//.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
tabBar.items?[1].title = "length"
TabThird = self.tabBar.items![2]
TabThird.image = UIImage(named: "btn_3-1")!//.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
tabBar.items?[2].title = "length"
TabForth = self.tabBar.items![3]
TabForth.image = UIImage(named: "btn_4-1")!//.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
tabBar.items?[3].title = "length"
TabFifth = self.tabBar.items![4]
TabFifth.image = UIImage(named: "btn_5-1")!//.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
tabBar.items?[4].title = "length"
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
switch item.tag{
case 0:
print("FirstTab")
UITabBar.appearance().tintColor = UIColor(red: 255/255.0, green: 67/255.0, blue: 99/255.0, alpha: 1.0)
case 1:
print("SecondTab")
UITabBar.appearance().tintColor = UIColor(red: 237/255.0, green: 193/255.0, blue: 53/255.0, alpha: 1.0)
case 2:
print("ThirdTab")
UITabBar.appearance().tintColor = UIColor(red: 70/255.0, green: 183/255.0, blue: 128/255.0, alpha: 1.0)
case 3:
print("ForthTab")
UITabBar.appearance().tintColor = UIColor(red: 12/255.0, green: 195/255.0, blue: 199/255.0, alpha: 1.0)
case 4:
print("FifthTab")
UITabBar.appearance().tintColor = UIColor(red: 105/255.0, green: 72/255.0, blue: 170/255.0, alpha: 1.0)
default:
break
}
}
override func viewWillAppear(_ animated: Bool) {
UIApplication.shared.isStatusBarHidden = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
最佳答案
编辑:您的 switch 语句中缺少中断:
switch item.tag{
此外,您正在对标签进行切换,但我没有看到您在代码中对它们进行相应标记的任何地方。您应该获取项目的索引。
我不是 Swift 程序员,这是你在 Objective-C 中的做法,给你一个提示:
NSInteger indexOfTab = [[self.tabBar items] indexOfObject:item];
然后执行 indexOfTab 的 switch 语句。
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("the selected index is : \(tabBar.items.index(of: item))")
}
如果你想单独改变“tintColor”,你应该设置一个自定义selectedImage相反。
注意:
By default, unselected and selected images are automatically created from the alpha values in the source images. To prevent system coloring, provide images with alwaysOriginal.
至于documentation是的,UITabBarItem 没有“tintColor”属性。
然而,UITabBar本身有一个 tintColor property .但这是不单独设置任何东西。
Tint Color
You can specify a custom tint color for the bar background using the Tint (barTintColor) field. The default background tint color is white.
Use the Image Tint (selectedImageTintColor) field to specify the bar item’s tint color when that tab is selected. By default, that color is blue.
关于调整大小的方法,您应该 resize your original image相反,或者 check this question如果它确实满足您的需求。但是,UITabBar 和 UITabBarItem 自定义仅限于您可以在文档中阅读的内容。
如果您想进一步单独定制事物,我建议您改为搜索或创建定制解决方案。
关于swift - 如何自定义 tintColor 和调整 UITabBarItem 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42038402/
根据标题。 使用外观代理设置我的app tint color有什么好处[[UINavigationBar appearance] setTintColor: 相对于 self.view.tintCol
有没有人设法为 UISearchBar 的范围栏部分着色,它有一个 tintColor 属性,但设置它不会影响附加的范围栏 UISegmentedControl。我在酒吧上有一个 Handlebars
我尝试了很多方法,但都没有解决我的问题。我收到警告: Failed to set (tintColor) user defined inspected property on (UINavigatio
我有一个自定义图标(.png 图像)作为栏按钮,我将使用 Theme.swift 文件为 1 个特定 View Controller 设置资源颜色/背景颜色,但由于某种原因,色调颜色不是正在更新。 当
QLPreviewController * preview = [[QLPreviewController alloc] init]; preview.dataSource = self; previ
我在让 UISegmentedControl 显示所需的色调时遇到问题。 // AppDelegate - (BOOL)application:(UIApplication *)application
我将窗口的色调颜色设置为任意颜色,然后尝试在每个按钮的基础上覆盖它,但似乎只要应用了 segue,按钮就会恢复为窗口色调颜色在他们身上。 在 didFinishLaunchingWithOptions
我正在从服务器接收图像,然后根据用户选择的颜色,图像颜色将被更改。 我尝试了以下方法: _sketchImageView.image = [_sketchImageView.image imageWi
我正在尝试更改 watch 应用程序复杂功能(模块化大型高身)中的文本颜色,但无论我做什么,文本都保持白色。 这是我的代码,包含 tintColor 的行,我已经一起尝试了它们,并且每个都一个接一个。
我有 4 个图标。我希望它们在选择时具有不同的“突出显示”颜色。 我怎样才能实现这个目标? 最佳答案 在您呈现的VC中的viewWillAppear中: override func viewWillA
出于某种原因,我无法在 View 上使用 UINavigationBar,而是使用 UIToolBar。我使用带有清晰 tintColor 的虚拟左按钮和带有黑色 tintColor 的标题按钮来正确
我为 UINavigationController 创建了一个子类,我在其中更改了栏的颜色。 - (void)viewDidLoad { [super viewDidLoad]; se
我正在尝试将所选段的 UIsegmentedControl 色调颜色设置为渐变颜色,但我无法做到这一点 我正在尝试关注这篇文章 https://www.bethedev.com/2019/02/set
我正在使用以下代码创建一个 UIBarButtonItem: UIBarButtonItem* searchBarButton = [[[UIBarButtonItem alloc] initWith
我正在尝试使用自定义颜色更改标签栏的颜色,但似乎没有办法... 我以这种方式为 UIColor 类定义了一个类别: @implementation UIColor (UakariColor) +(UI
我的 SearchBar 的 TintColor 有问题。我在导航栏和搜索栏中使用相同的 RGB 颜色。正如您在下面看到的,颜色不一样。 这是我用来设置导航栏 TintColor 的代码: s
我的类(class)有一个 tintColor 函数: func pinColor() -> UIColor { switch status.text! { case "online
我的导航栏中有一个 UIBarButton,我从 Storyboard 中为其设置了一个图像 (silhouette.png),并且我可以随意更改该图像的色调(颜色): if let num2
我有一个 UIBarButtonItem 定义如下: let cardNavButton: UIBarButtonItem = UIBarButtonItem(image: UIImage(named
我有一个继承自 UIButton 的自定义 class。我想要完成的事情是根据按钮的启用状态(即启用或禁用)设置 tintColor 属性。 有什么办法可以实现吗? 这是我的课: class Butt
我是一名优秀的程序员,十分优秀!