2011-08-30 4 views

답변

6
[mBtn setTintColor:[UIColor grayColor]]; 

이것은 강조 표시된 상태에만 영향을 주므로, 이것이 사용자가 찾고있는 것입니다.

Highlight Tint 드롭 다운 메뉴에서 Interface Builder로 설정할 수도 있습니다.

+0

'UIButton'이 '-setTintColor :'에 응답하지 않을 수 있습니다. 'UTutton'이 'setTintColor :'에 응답하지 않을 수 있습니다. – Syntax

+0

맞습니다. 다시 확인했는데 이것은 iOS에서만 작동합니다. 5.0 이상. 이 경우 UIView를 만들고 배경색을 설정 한 다음이보기를 강조 표시된 상태의 배경 이미지로 사용해야합니다. – antalkerekes

+0

그게 전부입니다. iOS 5를 출시하겠습니다. – Syntax

0

이 같이있는 방법이 없다, setBackgroundColor : forState :

확인 설명서를 참조하십시오. 당신은 이미지를 사용해야합니다.

+1

그 다음에는 색이 바뀌기 만하면 이미지를로드해야합니다. – Syntax

+0

@syntax 죄송합니다. 그러면 옵션이 없습니다. – Ishu

10

이 문제에 대한 해결책을 찾기 위해 지속적으로 팝업을 표시하고 다른 해결책을 찾지 못했기 때문에이 오래된 스레드에 답장을 보내고 있습니다. setTintColor는 UIButton의 강조 표시된 상태에만 적용됩니다. 6 개월 전, iOS 5에만 적용되는 것은 똑같이 성가 셨지만, 앞으로는 더 이상 문제가되지 않을 것입니다. 이 점을 염두에두고, 일반적인 아이디어를 조합하여 일반적인 상태의 버튼 그룹에 색을 칠하는 몇 가지 커뮤니티 제안을 모았습니다.

아래의 방법은 UIButtons의 NSArray와 색상 지정 세트를 입력으로 허용합니다. setTintColor를 사용하여 하나의 버튼에 색상 사양을 적용하고 결과를 UIImage로 렌더링 한 다음 해당 이미지를 전체 버튼 세트의 배경 이미지로 적용합니다. 이렇게하면 단추 색상에 대한 개별 이미지 파일을 만들지 않아도됩니다. 또한 다양한 크기의 단추 컬렉션에서 작동 할 수 있도록 이미지를 사용하여 이미지를 확대합니다 (UIButton의 기본 모서리 반올림 요소를 가정 함). iOS 5 타겟에서 유용하게 사용하시기 바랍니다. 그냥 내가 강조 상태에 대한 배경 색상을 변경하기위한 검색 않았을 때처럼 여기에 착륙 할 것이다 사람들을위한

- (void) setColorOfButtons:(NSArray*)buttons red:(float)red green:(float)green blue:(float)blue alpha:(float)alpha { 

    if (buttons.count == 0) { 
     return; 
    } 

    // get the first button 
    NSEnumerator* buttonEnum = [buttons objectEnumerator]; 
    UIButton* button = (UIButton*)[buttonEnum nextObject]; 

    // set the button's highlight color 
    [button setTintColor:[UIColor colorWithRed:red/255.9999f green:green/255.9999f blue:blue/255.9999f alpha:alpha]]; 

    // clear any existing background image 
    [button setBackgroundImage:nil forState:UIControlStateNormal]; 

    // place the button into highlighted state with no title 
    BOOL wasHighlighted = button.highlighted; 
    NSString* savedTitle = [button titleForState:UIControlStateNormal]; 
    [button setTitle:nil forState:UIControlStateNormal]; 
    [button setHighlighted:YES]; 

    // render the highlighted state of the button into an image 
    UIGraphicsBeginImageContext(button.layer.frame.size); 
    CGContextRef graphicsContext = UIGraphicsGetCurrentContext(); 
    [button.layer renderInContext:graphicsContext]; 
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIImage* stretchableImage = [image stretchableImageWithLeftCapWidth:12 topCapHeight:0]; 
    UIGraphicsEndImageContext(); 

    // restore the button's state and title 
    [button setHighlighted:wasHighlighted]; 
    [button setTitle:savedTitle forState:UIControlStateNormal]; 

    // set background image of all buttons 
    do { 
     [button setBackgroundImage:stretchableImage forState:UIControlStateNormal]; 
    } while (button = (UIButton*)[buttonEnum nextObject]);  
} 
+1

'[button titleForState : UIControlStateNormal]'이 설정되지 않았을 때 (예 : IB를 통해 작업 할 때) 유효하지 않은 객체를 반환 할 수 있습니다.이 행을 NSString savedTitle = [button titleLabel] .text;로 변경하면 작동합니다. 정확히 어떤 수표가 있어야하는지 잘 모르겠지만 수표를 추가하고 싶을 수도 있습니다. 왜냐하면 그 수표는 무효로 돌아 가지 않기 때문에 무효 인 객체를 반환하고 제목이 설정되지 않은 경우 충돌이 발생합니다. –

+0

Tipp ishaq에 감사드립니다. 똑같은 문제에 직면 해 있었는데 ... –

4

... 내가 backgroundHighlightColor 트랙을 통해 강조에 대한 속성이있는있는 UIButton 서브 클래스로 결국

KVO. 여기에 GitHub에 대한 링크가 있습니다 : SOHighlightButton

강조 표시되어있을 경우 UIButton의 기타/기타 속성이 필요할 경우 다른 시나리오에 적용 할 수 있어야합니다.