2017-10-12 1 views
0

나는 toggle이 선택된 투명한 이미지가있는 UIButton이 있습니다. 현재 구성에서 버튼을 멋지게 반전시키기 때문에 시스템 버튼 유형을 사용하고 있습니다. 그러나이 반전과 함께, 나는 테두리가있는 이미지를 얻었고이 이미지가 전체 버튼을 채우기를 원합니다. 어떻게이 일을 성취 할 수 있습니까? 나는 시스템에서 투명 마스크를 유지하고 싶습니다. 아래 이미지에서iOS - UIButton 시스템 선택된 이미지

Here is the repository.

선택한 시스템 상태의 모습입니다.

enter image description here

+0

을 템플릿에 이미지의 옵션을 렌더링 설정해야 한 가지 더있다? –

+0

전체 단추 프레임을 채우기 위해 iOS가 선택한 단추를 반전시키기 위해 추가하는 둥근 사각형이 필요합니다. –

+0

sry, 나에게 명확하지 않습니다. –

답변

0

class CustomButton: UIButton { 

    /* 
    // Only override draw() if you perform custom drawing. 
    // An empty implementation adversely affects performance during animation. 
    override func draw(_ rect: CGRect) { 
     // Drawing code 
    } 
    */ 




    override var isSelected: Bool { 


     didSet { 
      if isSelected { 
       self.backgroundColor = UIColor.blue 
       self.tintColor = UIColor.white 
      } else { 
       self.backgroundColor = .white 
       self.tintColor = UIColor.red 
      } 

     } 
    } 

} 

색 변경 요구 사항

그리고 뷰 컨트롤러에서

에 따른이 좋아합니까

의 경우에 isSelected

을있는 UIButton의 서브 클래스를 만들고 오버라이드 (override)

class ViewController: UIViewController { 

@IBOutlet weak var button: CustomButton! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    button.isSelected = false 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
@IBAction func btnTapped(_ sender: Any) { 
    button.isSelected = !button.isSelected 
} 

}

을 그리고 당신은 이미지와 버튼에서 원하는 무엇 Template Image

+0

흰색 배경의 경우이 방법이 효과적이지만 이미지 배경은 어떻게됩니까? –

+0

당신은 그것들을 하얗게 만들고 런타임에 색을 적용해야합니다. –

관련 문제