버튼 모서리를 둥글게하는 방법
직사각형 이미지 (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
create category class of CALayer
in h file
@property(nonatomic, assign) UIColor* borderIBColor;
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:
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
'Programming' 카테고리의 다른 글
인터넷에 액세스 할 수없는 Android 에뮬레이터 (0) | 2020.05.21 |
---|---|
각도 포트를 4200에서 다른 포트로 변경하는 방법 (0) | 2020.05.21 |
cocoapods 버전 1.0.0.beta.1에서 포드 설치 표시 오류 (0) | 2020.05.21 |
Docker에서 디렉토리 변경 명령? (0) | 2020.05.21 |
한 요소가 다른 요소에 포함되어 있는지 Javascript를 확인하는 방법 (0) | 2020.05.21 |