How to change the corner radius of UISegmentedCont

2019-03-09 08:02发布

问题:

Is it possible to change the corner radius of UISegmentedControl? I have tried the following approach which we use to change a UIView's corner radius.

    self.segmentedControl.layer.cornerRadius = 15.0;
    self.segmentedControl.layer.masksToBounds = YES;

This did not work as you can see it only cuts off the UISegmentedControl's corner.

Thanks!

回答1:

This should work:

self.segmentedControl.layer.cornerRadius = 15.0;
self.segmentedControl.layer.borderColor = [UIColor whiteColor].CGColor;
self.segmentedControl.layer.borderWidth = 1.0f;
self.segmentedControl.layer.masksToBounds = YES;

You need to specify the border after setting cornerRadius.



回答2:

Embed UISegmentedControl inside UIView and set corner radius for UIView.

Objective-C

outerView.layer.cornerRadius = CGRectGetHeight(outerView.bounds) / 2;
outerView.layer.borderColor = [UIColor blueColor].CGColor;
outerView.layer.borderWidth = 1;

Swift

outerView.layer.cornerRadius = CGRectGetHeight(outerView.bounds) / 2
outerView.layer.borderColor = UIColor.blueColor().CGColor
outerView.layer.borderWidth = 1



回答3:

The segmented control is not going to change the way it draws its corners, so it is continuing to draw its corners in its own way and you are then cutting them off. You are not in charge of how a segmented control draws its boundary shape. If you truly don't like the way it's drawn, you'll have to devise your own substitute control from scratch. The closest you can legitimately come to the kind of thing you're trying to do is to set the segmented control's background image.



回答4:

Your result is because something other (custom drawing?) controls the border and not the layer. Luckily it seems that that layer settings have priority.

If you know what border color you need, you can just add (example):

self.segmentedControl.layer.borderColor = [UIColor whiteColor].CGColor;
self.segmentedControl.layer.borderWidth = 1.0;


回答5:

Updated for Swift 3 & Xcode 8.2 compatibility

    mySegmentedControl.layer.cornerRadius = 25.0
    mySegmentedControl.layer.borderColor = UIColor.white.cgColor
    mySegmentedControl.layer.borderWidth = 1.0
    mySegmentedControl.layer.masksToBounds = true


回答6:

The previous solutions never worked for me. My solution is:

To embed the UISegmentedControl inside a superview, then assign -1 to the constraints leading, trailing, bottom, top, in order to cut off the UISegmentedControl border. Finally the superview should be configured in this way:

segmentedControl.superview.clipsToBounds = true
segmentedControl.superview.layer.cornerRadius = 0 //whatever
segmentedControl.superview.layer.borderWidth = 1
segmentedControl.superview.layer.borderColor = segmentedControl.tintColor.CGColor


回答7:

This is a converted and working code for Swift 4.1 and Xcode 9.3 of Yakiv's post.

segmentedOuterView.layer.cornerRadius = segmentedOuterView.bounds.height / 2
segmentedOuterView.layer.borderColor = UIColor.red.cgColor
segmentedOuterView.layer.borderWidth = 1
segmentedOuterView.layer.masksToBounds = true


回答8:

Use the following code :

segmentContrl.layer.borderColor=*anycolor*.CGColor;
segmentContrl.layer.cornerRadius = 0.0;
segmentContrl.layer.borderWidth = 1.5f;