사용자 지정 UIButton의 배경색을 변경하는 방법을 아는 사람이 있습니까? 당신은 이미지에서 볼 수 있듯이단추를 눌렀을 때 사용자 지정 UIButton (CoreGraphics)의 배경색을 변경하십시오.
플롯이라는 버튼이 주황색의 배경 색상을 가지고있다. 그러나 왼쪽 버튼 (검은 색 삼각형)은 주황색이 아닙니다.
내가 원하는 것은 "plot"또는 "award"버튼을 누르면 그 옆의 삼각형 버튼도 함께 변경됩니다.
func drawTriangle() {
//Move draw rect code into here
}
과의 drawRect에서이 함수를 호출 :
@IBDesignable class LeadingButton: UIButton {
override func drawRect(rect: CGRect) {
// *Draw triangle button:
// Create context for triangle
let context = UIGraphicsGetCurrentContext()
// *Put information into context:
// Info for drawing triangle:
CGContextMoveToPoint(context, 17, 0)
CGContextAddLineToPoint(context, 0, 40)
CGContextAddLineToPoint(context, 17, 40)
CGContextAddLineToPoint(context, 17, 0)
// Info color triangle
CGContextSetFillColorWithColor(context, UIColor.blackColor().CGColor)
// Color in triangle with specified color
CGContextFillPath(context)
// Draw triangle
CGContextStrokePath(context)
}
}
class MovieDetailController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// For changing background color of buttons
self.buttonArray = [plotB, directorB, writerB, actorsB, countryB, awardB, posterB, leadingB, trailingB]
}
func isButton(pressed: Int) {
switch pressed {
case 0...8:
let selectedButton = buttonArray[pressed]
selectedButton.backgroundColor = UIColor.orangeColor()
var idDictionary = [0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6]
idDictionary.removeValueForKey(pressed)
for (_, value) in idDictionary {
if value == 6 {
}
else {
let remainButtons = buttonArray[value]
remainButtons.backgroundColor = UIColor.blackColor()
}
}
default:
print("test")
}
}
시도했지만 작동하지 않았습니다. 디버그 콘솔에서도 경고 메시지가 나타납니다. : CGContextDrawPath : 컨텍스트 0x0이 잘못되었습니다. 백 트레이스를보고 싶다면 CG_CONTEXT_SHOW_BACKTRACE 환경 변수를 설정하십시오. 언제든지 drawRect를 호출 할 수 없기 때문입니까? 나는 그것을 어딘가에 읽었다. –
Kelvin