2017-05-03 11 views
0

나는 세 개의 탭이있는 UITabBar를 가지고 있습니다. 이제 할당하거나 하나의 탭의 전체 너비를 관련 selectionIndicatorImage에 채우라고 말하고 싶습니다. 현재 탭이 선택된 경우 테두리가 있습니다. 왼쪽에있는 탭은 다음과 같은 화면에 보여줍니다처럼 :의 backgroundColor를 위해스위프트 3 - selectionIndicatorImage에 대한 UITabBarItem의 전체 너비

var activeItemBackground:UIColor = UIColor.white { 
    didSet { 

     let numberOfItems = CGFloat((items!.count)) 

     let tabBarItemSize = CGSize(width: frame.width/numberOfItems, 
            height: frame.height) 

     selectionIndicatorImage = UIImage.imageWithColor(color: activeItemBackground, 
            size: tabBarItemSize).resizableImage(withCapInsets: .zero) 

     frame.size.width = frame.width + 4 
     frame.origin.x = -2 


    } 
} 

그리고있는 UIImage-확장 :

border

나는 새로운 속성의 UITabBar의 서브 클래스를 만들어 및 이미지 :

이 문제에 대해 많은 내용을 읽었지만 불행히도 제대로 작동하지 않습니다. 내 코드에 뭔가 빠졌습니까? 당신이 필요해서는 안

답변

1

나는, 당신이 몇 가지 추가 단계 ...

당신은 탭 표시 줄 항목의 정확한 크기를 계산하고, 그 크기의 이미지를 만드는을 가지고 있다고 생각 .resizableImage 부분.

정확한 크기로 설정 중이므로 탭 막대 프레임의 크기를 조정하지 않아도됩니다.

이것은 (당신의 .imageWithColor FUNC를 사용) 내 테스트에서 잘 작동하는 표시 : 그럼 첫 번째 VC의있는 viewDidLoad에서

class MyTabBar: UITabBar { 

    var activeItemBackground:UIColor = UIColor.white { 
     didSet { 

      let numberOfItems = CGFloat((items!.count)) 

      let tabBarItemSize = CGSize(width: frame.width/numberOfItems, 
             height: frame.height) 

      selectionIndicatorImage = UIImage.imageWithColor(color: activeItemBackground, 
                  size: tabBarItemSize) 

     } 
    } 

} 

:

if let tb = self.tabBarController?.tabBar as? MyTabBar { 
     tb.activeItemBackground = UIColor.red 
    } 
+0

감사합니다. 코드가 제대로 작동합니다. 내 잘못은 내 코드의 초기에 activeItemBackground를 설정 한 마지막 코드 스 니펫 때문이었습니다. –