문제

환경:-xcode 6 베타 4- 신속한 언어 - iOS Tabbed Application (기본 Xcode 프로젝트)

탭의 기본 회색을 다른 색상으로 어떻게 변경할 수 있나요?(바람직하게는 전 세계적으로)

내 연구에 따르면 각 탭의 이미지 렌더링 모드를 원본 렌더링 모드로 변경해야 하는데 어떻게 해야 하는지 모르겠습니다.

도움이 되었습니까?

해결책

각 (기본값) 탭 표시 줄 항목은 텍스트와 아이콘으로 구성됩니다.외관을 지정하여 텍스트 색상을 전 세계적으로 변경하는 것은 꽤 쉽습니다.

// 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]
.

대신

for item in self.tabBar.items as [UITabBarItem]
.

도움이되기를 바랍니다!

스위프트 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)

스위프트 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
 }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top