How to make an UIView into circle shape in swift 4.2?
The width(40) and height(40) of the UIView are equal.
I have tried these but they didn't work for me:
countView?.clipsToBounds = true
self.countView?.layer.cornerRadius = min((countView?.frame.size.width)!, (countView?.frame.size.height)!)/2
and also
self.countView.layer.cornerRadius = (countView.frame.size.width)/2
Thanks
ios swift
add a comment |
The width(40) and height(40) of the UIView are equal.
I have tried these but they didn't work for me:
countView?.clipsToBounds = true
self.countView?.layer.cornerRadius = min((countView?.frame.size.width)!, (countView?.frame.size.height)!)/2
and also
self.countView.layer.cornerRadius = (countView.frame.size.width)/2
Thanks
ios swift
What do you get now?
– Rakesha Shastri
Nov 25 '18 at 7:41
UIView have layer indeed is it shapeable tho, depends on whats inside the bounds of that View, and there constraints and frames
– Mohmmad S
Nov 25 '18 at 7:55
First printcountView.frame.size.widthand see what is the value.
– Kamran
Nov 25 '18 at 8:00
add a comment |
The width(40) and height(40) of the UIView are equal.
I have tried these but they didn't work for me:
countView?.clipsToBounds = true
self.countView?.layer.cornerRadius = min((countView?.frame.size.width)!, (countView?.frame.size.height)!)/2
and also
self.countView.layer.cornerRadius = (countView.frame.size.width)/2
Thanks
ios swift
The width(40) and height(40) of the UIView are equal.
I have tried these but they didn't work for me:
countView?.clipsToBounds = true
self.countView?.layer.cornerRadius = min((countView?.frame.size.width)!, (countView?.frame.size.height)!)/2
and also
self.countView.layer.cornerRadius = (countView.frame.size.width)/2
Thanks
ios swift
ios swift
edited Nov 25 '18 at 7:40
Rakesha Shastri
7,39621435
7,39621435
asked Nov 25 '18 at 7:38
B SAI DEERAJB SAI DEERAJ
23
23
What do you get now?
– Rakesha Shastri
Nov 25 '18 at 7:41
UIView have layer indeed is it shapeable tho, depends on whats inside the bounds of that View, and there constraints and frames
– Mohmmad S
Nov 25 '18 at 7:55
First printcountView.frame.size.widthand see what is the value.
– Kamran
Nov 25 '18 at 8:00
add a comment |
What do you get now?
– Rakesha Shastri
Nov 25 '18 at 7:41
UIView have layer indeed is it shapeable tho, depends on whats inside the bounds of that View, and there constraints and frames
– Mohmmad S
Nov 25 '18 at 7:55
First printcountView.frame.size.widthand see what is the value.
– Kamran
Nov 25 '18 at 8:00
What do you get now?
– Rakesha Shastri
Nov 25 '18 at 7:41
What do you get now?
– Rakesha Shastri
Nov 25 '18 at 7:41
UIView have layer indeed is it shapeable tho, depends on whats inside the bounds of that View, and there constraints and frames
– Mohmmad S
Nov 25 '18 at 7:55
UIView have layer indeed is it shapeable tho, depends on whats inside the bounds of that View, and there constraints and frames
– Mohmmad S
Nov 25 '18 at 7:55
First print
countView.frame.size.width and see what is the value.– Kamran
Nov 25 '18 at 8:00
First print
countView.frame.size.width and see what is the value.– Kamran
Nov 25 '18 at 8:00
add a comment |
2 Answers
2
active
oldest
votes
You can use a mask layer like this:
override func viewDidLoad() {
super.viewDidLoad()
let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(ovalIn: self.countView.bounds).cgPath;
self.countView.layer.mask = maskLayer
}
Example Output
Here the output of a UIView with width/height = 128/128:

add a comment |
In addition to @Stephan answer.
With less code:
self.countView.layer.cornerRadius = self.countView.bounds.size.height / 2
Result:

add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465559%2fhow-to-make-an-uiview-into-circle-shape-in-swift-4-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use a mask layer like this:
override func viewDidLoad() {
super.viewDidLoad()
let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(ovalIn: self.countView.bounds).cgPath;
self.countView.layer.mask = maskLayer
}
Example Output
Here the output of a UIView with width/height = 128/128:

add a comment |
You can use a mask layer like this:
override func viewDidLoad() {
super.viewDidLoad()
let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(ovalIn: self.countView.bounds).cgPath;
self.countView.layer.mask = maskLayer
}
Example Output
Here the output of a UIView with width/height = 128/128:

add a comment |
You can use a mask layer like this:
override func viewDidLoad() {
super.viewDidLoad()
let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(ovalIn: self.countView.bounds).cgPath;
self.countView.layer.mask = maskLayer
}
Example Output
Here the output of a UIView with width/height = 128/128:

You can use a mask layer like this:
override func viewDidLoad() {
super.viewDidLoad()
let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(ovalIn: self.countView.bounds).cgPath;
self.countView.layer.mask = maskLayer
}
Example Output
Here the output of a UIView with width/height = 128/128:

answered Nov 25 '18 at 8:08
Stephan SchlechtStephan Schlecht
5,0151911
5,0151911
add a comment |
add a comment |
In addition to @Stephan answer.
With less code:
self.countView.layer.cornerRadius = self.countView.bounds.size.height / 2
Result:

add a comment |
In addition to @Stephan answer.
With less code:
self.countView.layer.cornerRadius = self.countView.bounds.size.height / 2
Result:

add a comment |
In addition to @Stephan answer.
With less code:
self.countView.layer.cornerRadius = self.countView.bounds.size.height / 2
Result:

In addition to @Stephan answer.
With less code:
self.countView.layer.cornerRadius = self.countView.bounds.size.height / 2
Result:

answered Nov 25 '18 at 8:46
excitedmicrobeexcitedmicrobe
1,1131519
1,1131519
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465559%2fhow-to-make-an-uiview-into-circle-shape-in-swift-4-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you get now?
– Rakesha Shastri
Nov 25 '18 at 7:41
UIView have layer indeed is it shapeable tho, depends on whats inside the bounds of that View, and there constraints and frames
– Mohmmad S
Nov 25 '18 at 7:55
First print
countView.frame.size.widthand see what is the value.– Kamran
Nov 25 '18 at 8:00