How to change the corner radius of UISegmentedCont

2019-03-09 08:25发布

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. enter image description here

Thanks!

8条回答
唯我独甜
2楼-- · 2019-03-09 09:07

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;
查看更多
一纸荒年 Trace。
3楼-- · 2019-03-09 09:09

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
查看更多
登录 后发表回答