SWIFT Xcode 6を使用したデフォルトタブバー項目色
質問
環境: - Xcode 6 Beta 4 - 迅速な言語 - iOSタブ付きアプリケーション(デフォルトのXcode Project)
タブのデフォルトの灰色の色を他のものに変更するにはどうすればよいですか。(好ましくは世界的に)
私の研究が行われる限り、私はどういうわけか元のレンダリングモードに画像レンダリングモードを変更する必要がありますが、
がわかりません解決
各(デフォルト)タブバー項目はテキストとアイコンで構成されています。外観を指定してグローバルにテキストの色を変更するのはとても簡単です:
// you can add this code to you AppDelegate application:didFinishLaunchingWithOptions:
// or add it to viewDidLoad method of your TabBarController class
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.magentaColor()], forState:.Normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState:.Selected)
.
画像がある状況が少し複雑です。グローバルに外観を定義することはできません。TabBarControllerクラスでそれらを再定義する必要があります。viewDidLoad
クラスのTabBarController
メソッドにコードを追加する:
for item in self.tabBar.items as [UITabBarItem] {
if let image = item.image {
item.image = image.imageWithColor(UIColor.yellowColor()).imageWithRenderingMode(.AlwaysOriginal)
}
}
.
UIImageクラスには、imageWithColor(...)
メソッドがありません。そのため、ここでは拡張実装があります:
// Add anywhere in your app
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, .Normal)
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
}
}
.
imageWithColor
はこの回答から借用されました。 https://stackoverflow.com/a/24545102/3050466
他のヒント
コメントをコメントするのに十分な評判はありませんが、多くの興味がある選択した画像
の色を変更する方法は興味があります。の後に別の
if let
チェックを追加するだけです
if let image = item.image
.
このように:
if let selectedImage = item.selectedImage {
item.selectedImage = selectedImage.imageWithColor(UIColor.yellowColor()).imageWithRenderingMode(.AlwaysOriginal)
}
.
これは完全に問題を解決しました。 そして、Swift 1.2とXcode 6.3.2以来、
が必要なので少し追加for item in self.tabBar.items as! [UITabBarItem]
.
の代わりにp>
for item in self.tabBar.items as [UITabBarItem]
.
助けを願っています!
swift 2.0
タブバー画像のデフォルトの色を変更するには、viewDidLoad
クラスのTabBarController
メソッドにコードを追加します。
for item in self.tabBar.items! as [UITabBarItem] {
if let image = item.image {
item.image = image.imageWithColor(UIColor.yellowColor()).imageWithRenderingMode(.AlwaysOriginal)
}
}
.
imageWithColor
拡張子を更新します。上記の方法で使用され、TabBarController
クラスの外側に配置する必要があります。
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, CGBlendMode.Normal)
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
}
}
.
テキストの色が着色されるように変更されていませんが、参照のためだけに変更されません。viewDidLoad
:
// you can add this code to you AppDelegate application:didFinishLaunchingWithOptions:
// or add it to viewDidLoad method of your TabBarController class
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.magentaColor()], forState:.Normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState:.Selected)
. swift 3.0
タブバー画像のデフォルトの色を変更するには、viewDidLoad
クラスのTabBarController
メソッドにコードを追加します。
for item in self.tabBar.items! as [UITabBarItem] {
if let image = item.image {
item.image = image.imageWithColor(tintColor: UIColor.yellow).withRenderingMode(.alwaysOriginal)
}
}
.
imageWithColor
拡張子を更新します。上記の方法で使用され、TabBarController
クラスの外側に配置する必要があります。
extension UIImage {
func imageWithColor(tintColor: 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!)
tintColor.setFill()
context.fill(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()! as UIImage
UIGraphicsEndImageContext()
return newImage
}
}
.