UIButton 강조 색상 지정
그럼 조사 과정 순서대로 열거하고 싶습니다.
색상을 직접 지정할 수 있는 방법 없을까요?
처음에는 보통 setBackgroundColor(color: UIColor, forState state: UIControlState) 방법이 있다고 생각했지만, UIButton은 그런 방법이 없었다.
밝을 때 방법을 정하지 않았나요?
UIButton반showsTouchWhenHighlighted의 순간이런 방법이 있지만 이것은 하이라이트 상태에서 로우의 피드백을 회답하는 것이다. 내가 기대했던 것과 다르다.나는 전체 단추의 색을 바꾸고 싶다.
↓ showsTouchWhen Highlighted=진짜상황
state를 통해 값을 지정할 수 있는 방법이 있습니까
컬러는 아니지만 state를 통해 이미지 setBackgroundImage(image: UIImage?, forState state: UIControlState) 를 지정할 수 있는 방법을 찾았습니다.
그럼 UIColor를 UIImage로 바꿔서 지목하면 되는 거죠?해봤어.
UIColor→UIImage로 변환
여기.를 참고로 하거나 직접 사용합니다.private func createImageFromUIColor(color: UIColor) -> UIImage {
// 1x1のbitmapを作成
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
// bitmapを塗りつぶし
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
// UIImageに変換
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
. 정규 색상을 지정합니다.
지금까지 이렇게 지목해 주셨어요.button.backgroundColor = UIColor.redColor()내가 이렇게 바꿔줄게.button.setBackgroundImage(self.createImageFromUIColor(UIColor.redColor()), forState: .Normal)이때 하이라이트의 색상은 변경됩니다.
다만, 고광선이 파란색으로 변하는 조건이 충족되지 않아 조금 더 추가했다.
. Highlighted를 지정할 때의 색상
나는 네가 이미 알고 있다고 생각한다. 그러나.정규 때와 같다.고급 lighted 색상을 지정합니다.
이런 느낌이야.button.setBackgroundImage(self.createImageFromUIColor(UIColor.blueColor()), forState: .Highlighted)이렇게 하면 의도대로 밝을 때 파란색으로 변한다.
총결산
코드를 게재하다.private var button: UIButton!
private func createImageFromUIColor(color: UIColor) -> UIImage {
// 1x1のbitmapを作成
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
// bitmapを塗りつぶし
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
// UIImageに変換
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
button = UIButton()
button.frame = CGRectMake(0,0,200,40)
button.layer.masksToBounds = true
button.setTitle("Tap!!!!!!!!", forState: UIControlState.Normal)
button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
button.setBackgroundImage(self.createImageFromUIColor(UIColor.redColor()), forState: .Normal)
button.setBackgroundImage(self.createImageFromUIColor(UIColor.blueColor()), forState: .Highlighted)
button.layer.cornerRadius = 5.0
let frame = self.view.frame
button.layer.position = CGPoint(x: frame.width/2, y:frame.height/2)
self.view.addSubview(button)
}
Reference
이 문제에 관하여(UIButton 강조 색상 지정), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/akatsuki174/items/c0b8b5126b6c12f62001
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
UIButton반
showsTouchWhenHighlighted의 순간이런 방법이 있지만 이것은 하이라이트 상태에서 로우의 피드백을 회답하는 것이다. 내가 기대했던 것과 다르다.나는 전체 단추의 색을 바꾸고 싶다.↓ showsTouchWhen Highlighted=진짜상황
state를 통해 값을 지정할 수 있는 방법이 있습니까
컬러는 아니지만 state를 통해 이미지 setBackgroundImage(image: UIImage?, forState state: UIControlState) 를 지정할 수 있는 방법을 찾았습니다.
그럼 UIColor를 UIImage로 바꿔서 지목하면 되는 거죠?해봤어.
UIColor→UIImage로 변환
여기.를 참고로 하거나 직접 사용합니다.private func createImageFromUIColor(color: UIColor) -> UIImage {
// 1x1のbitmapを作成
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
// bitmapを塗りつぶし
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
// UIImageに変換
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
. 정규 색상을 지정합니다.
지금까지 이렇게 지목해 주셨어요.button.backgroundColor = UIColor.redColor()내가 이렇게 바꿔줄게.button.setBackgroundImage(self.createImageFromUIColor(UIColor.redColor()), forState: .Normal)이때 하이라이트의 색상은 변경됩니다.
다만, 고광선이 파란색으로 변하는 조건이 충족되지 않아 조금 더 추가했다.
. Highlighted를 지정할 때의 색상
나는 네가 이미 알고 있다고 생각한다. 그러나.정규 때와 같다.고급 lighted 색상을 지정합니다.
이런 느낌이야.button.setBackgroundImage(self.createImageFromUIColor(UIColor.blueColor()), forState: .Highlighted)이렇게 하면 의도대로 밝을 때 파란색으로 변한다.
총결산
코드를 게재하다.private var button: UIButton!
private func createImageFromUIColor(color: UIColor) -> UIImage {
// 1x1のbitmapを作成
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
// bitmapを塗りつぶし
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
// UIImageに変換
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
button = UIButton()
button.frame = CGRectMake(0,0,200,40)
button.layer.masksToBounds = true
button.setTitle("Tap!!!!!!!!", forState: UIControlState.Normal)
button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
button.setBackgroundImage(self.createImageFromUIColor(UIColor.redColor()), forState: .Normal)
button.setBackgroundImage(self.createImageFromUIColor(UIColor.blueColor()), forState: .Highlighted)
button.layer.cornerRadius = 5.0
let frame = self.view.frame
button.layer.position = CGPoint(x: frame.width/2, y:frame.height/2)
self.view.addSubview(button)
}
Reference
이 문제에 관하여(UIButton 강조 색상 지정), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/akatsuki174/items/c0b8b5126b6c12f62001
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
private func createImageFromUIColor(color: UIColor) -> UIImage {
// 1x1のbitmapを作成
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
// bitmapを塗りつぶし
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
// UIImageに変換
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
코드를 게재하다.
private var button: UIButton!
private func createImageFromUIColor(color: UIColor) -> UIImage {
// 1x1のbitmapを作成
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
// bitmapを塗りつぶし
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
// UIImageに変換
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
button = UIButton()
button.frame = CGRectMake(0,0,200,40)
button.layer.masksToBounds = true
button.setTitle("Tap!!!!!!!!", forState: UIControlState.Normal)
button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
button.setBackgroundImage(self.createImageFromUIColor(UIColor.redColor()), forState: .Normal)
button.setBackgroundImage(self.createImageFromUIColor(UIColor.blueColor()), forState: .Highlighted)
button.layer.cornerRadius = 5.0
let frame = self.view.frame
button.layer.position = CGPoint(x: frame.width/2, y:frame.height/2)
self.view.addSubview(button)
}
Reference
이 문제에 관하여(UIButton 강조 색상 지정), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/akatsuki174/items/c0b8b5126b6c12f62001텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)