如何更改标签栏未选择的图标和文字的颜色?我找到了这个答案(How to change inactive icon/text color on tab bar?),但无法为swift实现它。
答案 0 :(得分:26)
iOS 10
class TabBarVC: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// make unselected icons white
self.tabBar.unselectedItemTintColor = UIColor.white
}
}
答案 1 :(得分:13)
以下设置所有UITabBarItem的默认值,您可以将其添加到AppDelegate
。它会改变你的文字颜色。
UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.blackColor()}, forState:.Selected)
UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.whiteColor()}, forState:.Normal)
用于更改图标'您可以将颜色设置为图像已经具有良好颜色的给定状态。
self.tabBarItem.selectedImage = [[UIImage imageNamed:@"selectedImage"]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem.image = [[UIImage imageNamed:@"notSelectedImage"]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
或者你可以这样做:
为UIImage
课程添加扩展程序(来自this answer):
extension UIImage {
func imageWithColor(color1: 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)
color1.setFill()
CGContextFillRect(context, rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
UIGraphicsEndImageContext()
return newImage
}
}
在您的viewDidLoad
:
for item in self.tabBar.items as [UITabBarItem] {
if let image = item.image {
item.image = image.imageWithColor(UIColor.blackColor()).imageWithRenderingMode(.AlwaysOriginal)
}
}
答案 2 :(得分:3)
在iOS 11中,您可以直接在情节提要中的UIToolBar上设置属性:
unselectedItemTintColor |颜色| [所需颜色]
Xcode打印
答案 3 :(得分:1)
补充@ BoilingLime的答案,这里是Swift 3中第二个替代版本的UIImage扩展:
extension UIImage {
func imageWithColor(color1: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
let context = UIGraphicsGetCurrentContext()! as CGContext
context.translateBy(x: 0, y: self.size.height)
context.scaleBy(x: 1.0, y: -1.0);
context.setBlendMode(CGBlendMode.normal)
let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
context.clip(to: rect, mask: self.cgImage!)
color1.setFill()
context.fill(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()! as UIImage
UIGraphicsEndImageContext()
return newImage
}
}
答案 4 :(得分:0)
如果您正在寻找iOS 11 swift 4解决方案,请在appDelegate中执行类似的操作。这会将所有未选中的选项卡栏项目更改为黑色。
public class MailNotification : INotification
{
public void Send(INotificationBaseModel notification)
{
try
{
SmtpClient client = new SmtpClient();
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add(notification.SendTo);
mailMessage.Subject = notification.Title;
mailMessage.Body = notification.Message;
mailMessage.IsBodyHtml = true;
client.Send(mailMessage);
}
catch (Exception ex)
{
throw new Exception("");
}
}
}
答案 5 :(得分:0)
快速4 +
UITabBar.appearance().unselectedItemTintColor = UIColor.green
在appDelegate's didFinishLaunchingWithOptions
方法中使用此代码。