[ios] 버튼 모서리를 둥글게하는 방법

직사각형 이미지 (jpg)가 있고 xcode에서 모서리가 둥근 버튼의 배경을 채우는 데 사용하고 싶습니다.

나는 다음과 같이 썼다.

UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
CGRect frame = CGRectMake(x, y, cardWidth, cardHeight);
button.frame = frame;
[button setBackgroundImage:backImage forState:UIControlStateNormal];

그러나 그 접근법으로 얻은 버튼에는 모서리가 둥글 지 않습니다. 대신 원래 이미지와 똑같이 보이는 일반 사각형입니다. 모서리가 둥근 이미지를 버튼 대신 표시하려면 어떻게해야합니까?

감사!



답변

나는 당신의 문제가 있다고 생각하고 UITextArea로 다음 솔루션을 시도했으며 이것이 UIButton에서도 작동한다고 생각합니다.

우선 .m 파일로 가져옵니다.

#import <QuartzCore/QuartzCore.h>

그런 다음 loadView메소드 에서 다음 줄을 추가 하십시오.

    yourButton.layer.cornerRadius = 10; // this value vary as per your desire
    yourButton.clipsToBounds = YES;

이것이 당신을 위해 작동하고 예라면 의사 소통을하기를 바랍니다.


답변

이 런타임 속성으로 달성 할 수 있습니다

여기에 이미지 설명을 입력하십시오

우리는 사용자 정의 버튼을 만들 수 있습니다. 첨부 된 스크린 샷을 참조하십시오.

친절하게주의하십시오 :

런타임 속성에서 테두리의 색을 변경하려면 다음 지침을 따르십시오.

  1. CALayer의 카테고리 클래스 작성

  2. h 파일에서

    @property(nonatomic, assign) UIColor* borderIBColor;
  3. m 파일에서 :

    -(void)setBorderIBColor:(UIColor*)color {
        self.borderColor = color.CGColor;
    }
    
    -(UIColor*)borderIBColor {
        return [UIColor colorWithCGColor:self.borderColor];
    }

이제 테두리 색상 확인 스크린 샷을 설정합니다

업데이트 된 답변

감사


답변

DCKit 이라는 내 라이브러리를 확인하고 싶을 수도 있습니다 . 최신 버전의 Swift 에 작성되었습니다 .

인터페이스 빌더에서 둥근 모서리 버튼 / 텍스트 필드를 직접 만들 수 있습니다.

DCKit : 둥근 버튼

또한 유효성 검사가 포함 된 텍스트 필드, 테두리가있는 컨트롤, 점선 테두리, 원 및 헤어 라인보기와 같은 다른 멋진 기능도 많이 있습니다.


답변

원을 얻기 위해 한계 모서리 반경까지 밀기 :

    self.btnFoldButton.layer.cornerRadius = self.btnFoldButton.frame.height/2.0;

여기에 이미지 설명을 입력하십시오

버튼 프레임이 정사각형 인 경우 frame.height 또는 frame.width는 중요하지 않습니다. 그렇지 않으면 두 가지 중 가장 큰 것을 사용하십시오.


답변

기존 프로젝트에 QuartCore 프레임 워크 가없는 경우 가져 오기를 수행 한 다음 viewcontroller.m#import <QuartzCore/QuartzCore.h> 에서 가져 오십시오 .

UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect]];
CGRect frame = CGRectMake(x, y, width, height); // set values as per your requirement
button.layer.cornerRadius = 10;
button.clipsToBounds = YES;


답변

UIButton* closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 50, 90, 35)];
//Customise this button as you wish then
closeBtn.layer.cornerRadius = 10;
closeBtn.layer.masksToBounds = YES;//Important


답변

먼저 버튼의 너비 = 100 및 높이 = 100을 설정하십시오.

목표 C 솔루션

YourBtn1.layer.cornerRadius=YourBtn1.Frame.size.width/2;
YourBtn1.layer.borderColor=[uicolor blackColor].CGColor;
YourBtn1.layer.borderWidth=1.0f;

스위프트 4 솔루션

YourBtn1.layer.cornerRadius = YourBtn1.Frame.size.width/2
YourBtn1.layer.borderColor = UIColor.black.cgColor
YourBtn1.layer.borderWidth = 1.0