Programming

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

procodes 2020. 5. 21. 21:42
반응형

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


직사각형 이미지 (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;

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


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

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

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

친절하게주의하십시오 :

in runtime attributes to change color of border follow this instruction

  1. create category class of CALayer

  2. in h file

    @property(nonatomic, assign) UIColor* borderIBColor;
    
  3. in m file:

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

now onwards to set border color check screenshot

업데이트 된 답변

thanks


You may want to check out my library called DCKit. It's written on the latest version of Swift.

You'd be able to make a rounded corner button/text field from the Interface builder directly:

DCKit : 둥근 버튼

It also has many other cool features, such as text fields with validation, controls with borders, dashed borders, circle and hairline views etc.


Pushing to the limits corner radius up to get a circle:

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

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

If button frame is an square it does not matter frame.height or frame.width. Otherwise use the largest of both ones.


Import QuartCore framework if it is not there in your existing project, then import #import <QuartzCore/QuartzCore.h> in viewcontroller.m

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

First set width=100 and Height=100 of button

Objective C Solution

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

Swift 4 Solution

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

If you want a rounded corner only to one corner or two corners, etc... read this post:

[ObjC] – UIButton with rounded corner - http://goo.gl/kfzvKP

It's a XIB/Storyboard subclass. Import and set borders without write code.


For Swift:

button.layer.cornerRadius = 10.0

updated for Swift 3 :

used below code to make UIButton corner round:

 yourButtonOutletName.layer.cornerRadius = 0.3 *
        yourButtonOutletName.frame.size.height

Try my code. Here you can set all properties of UIButton like text colour, background colour, corner radius, etc.

extension UIButton {
    func btnCorner() {
        layer.cornerRadius = 10
        clipsToBounds = true
        backgroundColor = .blue
    }
}

Now call like this

yourBtnName.btnCorner()

An alternative answer which sets a border too (making it more like a button) is here ... How to set rectangle border for custom type UIButton


iOS SWift 4의 경우

button.layer.cornerRadius = 25;
button.layer.masksToBounds = true;

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

대한 목표 C :

submitButton.layer.cornerRadius = 5;
submitButton.clipsToBounds = YES;

대한 스위프트 :

submitButton.layer.cornerRadius = 5
submitButton.clipsToBounds = true

참고 URL : https://stackoverflow.com/questions/5047818/how-to-round-the-corners-of-a-button

반응형