let tabbarItem0 = tabbar.items![0] as! UITabbarItemvar selectedImage0 : UIImage = UIImage(named:"ic_tabbar_item_one")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)var Fontlight:UIFont = UIFont(name: "HelveticaNeue-ultralight",size: 12)!var FontBold:UIFont = UIFont(name: "HelveticaNeue-Bold",size: 12)!tabbarItem0.image = unselectedImage0tabbarItem0.selectedImage = selectedImage0tabbarItem0.Title = "OvervIEw"tabbarItem0.setTitleTextAttributes( [ NSForegroundcolorAttributename: UIcolor.whitecolor(),NSFontAttributename: Fontlight ],forState: .normal)tabbarItem0.setTitleTextAttributes( [ NSForegroundcolorAttributename: UIcolor.whitecolor(),NSFontAttributename: FontBold ],forState: UIControlState.Selected)发现解决方案(Swift 3,XCode 8.1)
>在Storyboard中,为每个UITabbarItem提供一个唯一的标记:对于每个标签 – >选择它并转到它的“属性检查器” – >在“标签”字段中为每个人提供一个唯一的编号,但不应使用零(我使用1到4).
这为我们稍后设置,以确定按下哪个选项卡.
>创建一个新的UITabbarController子类,然后分配它:file – >新文件 – > iOS Cocoa touch – >创建一个UITabbarController的子类.将新的.swift文件分配给您的.swift文件
“IDentity Inspector”下的UITabbarController.
我们需要在UITabbarController中使用自定义逻辑.
>创建UITabbarItem的新子类,将相同的文件分配给所有UITabbarItem:file – >新文件 – > iOS Cocoa touch – >创建一个UITabbarItem的子类,并为所有选项卡分配相同的子类.
我们的标签栏项目中需要共享的自定义逻辑.
>将此代码添加到UITabbarItem子类,它设置初始状态(主选项卡粗体,其余未选中),并允许编程选项卡更改:
class MyUITabbarItemSubclass: UITabbarItem { //choose initial state Fonts and weights here let normalTitleFont = UIFont.systemFont(ofSize: 12,weight: UIFontWeightRegular) let selectedTitleFont = UIFont.systemFont(ofSize: 12,weight: UIFontWeightBold) //choose initial state colors here let normalTitlecolor = UIcolor.gray let selectedTitlecolor = UIcolor.black //assigns the proper initial state logic when each tab instantiates overrIDe func awakeFromNib() { super.awakeFromNib() //this tag # should be your primary tab's Tag* if self.tag == 1 { self.setTitleTextAttributes([NSFontAttributename: selectedTitleFont,NSForegroundcolorAttributename: selectedTitlecolor],for: UIControlState.normal) } else { self.setTitleTextAttributes([NSFontAttributename: normalTitleFont,NSForegroundcolorAttributename: normalTitlecolor],for: UIControlState.normal) } }}
这里我们设置初始状态,以便在应用程序打开时正确设置选项卡,我们将在下一个子类中处理物理选项卡.
>将此代码添加到UITabbarController子类中,这是在按下选项卡时分配正确状态的逻辑.
class MyUITabbarControllerSubclass: UITabbarController { //choose normal and selected Fonts here let normalTitleFont = UIFont.systemFont(ofSize: 12,weight: UIFontWeightRegular) let selectedTitleFont = UIFont.systemFont(ofSize: 12,weight: UIFontWeightBold) //choose normal and selected colors here let normalTitlecolor = UIcolor.gray let selectedTitlecolor = UIcolor.black //the following is a delegate method from the UITabbar protocol that's available //to UITabbarController automatically. It sends us information every //time a tab is pressed. Since we Tagged our tabs earlIEr,we'll kNow which one was pressed,//and pass that IDentifIEr into a function to set our button states for us overrIDe func tabbar(_ tabbar: UITabbar,dIDSelect item: UITabbarItem) { setbuttonStates(itemTag: item.tag) } //the function takes the tabbar.tag as an Int func setbuttonStates (itemTag: Int) { //making an array of all the tabs let tabs = self.tabbar.items //looPing through and setting the states var x = 0 while x < (tabs?.count)! { if tabs?[x].tag == itemTag { tabs?[x].setTitleTextAttributes([NSFontAttributename: selectedTitleFont,for: UIControlState.normal) } else { tabs?[x].setTitleTextAttributes([NSFontAttributename: normalTitleFont,for: UIControlState.normal) } x += 1 } }}
看起来这很痛苦,因为由于某种原因,标签不能将状态变化识别为“.Selected”.我们必须通过仅使用.normal状态来做所有事情 – 基本上自己检测状态变化.
>您可以通过编程方式更改选项卡并仍然通过…检测状态更改.如果有人有兴趣,我会稍后更新,只需询问.
希望这有帮助!
总结以上是内存溢出为你收集整理的在Swift for iOS中将TabBarItem标题字体更改为粗体全部内容,希望文章能够帮你解决在Swift for iOS中将TabBarItem标题字体更改为粗体所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)