ios 如何更改 UISegmentedControl 的圆角半径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23317645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:01:18  来源:igfitidea点击:

How to change the corner radius of UISegmentedControl?

ioscocoa-touchuiviewuikituisegmentedcontrol

提问by wz366

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.

是否可以更改 UISegmentedControl 的角半径?我尝试了以下方法来更改 UIView 的角半径。

    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

这不起作用,因为您可以看到它只切断了 UISegmentedControl 的角落。 在此处输入图片说明

Thanks!

谢谢!

回答by xi.lin

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.

设置cornerRadius后需要指定边框。

回答by Yakiv Kovalsky

Embed UISegmentedControl inside UIView and set corner radius for UIView.

在 UIView 中嵌入 UISegmentedControl 并为UIView设置圆角半径。

Objective-C

目标-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

Cornered UISegmentedControl

转角的 UISegmentedControl

回答by matt

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.

分段控件不会改变它绘制角的方式,因此它会继续以自己的方式绘制角,然后您将它们切断。您不负责分段控件如何绘制其边界形状。如果您真的不喜欢它的绘制方式,则必须从头开始设计自己的替代控件。您可以合法地尝试做的最接近的事情是设置分段控件的背景图像。

回答by deijmaster

Updated for Swift 3 & Xcode 8.2 compatibility

针对 Swift 3 和 Xcode 8.2 兼容性进行了更新

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

回答by Andrea Mugnaini

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

以前的解决方案对我不起作用。我的解决办法是:

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

要将UISegmentedControl内部嵌入超级视图,然后将 -1 分配给约束前导、尾随、底部、顶部,以切断UISegmentedControl边框。最后超级视图应该这样配置:

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

回答by Mihai Timar

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;

回答by Honey

There are some changes in iOS13. Hence you must set the borderRadius from within layoutSubviews:

iOS13 有一些变化。因此,您必须从内部设置 borderRadius layoutSubviews

override func layoutSubviews() {
    super.layoutSubviews()
    layer.cornerRadius = 2
}

回答by atereshkov

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

这是Yakiv 帖子的Swift 4.1 和 Xcode 9.3的转换和工作代码。

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

回答by Lloyd Keijzer

You're able to change the UISegmentedControlcornerRadius by increasing the layer its cornerRadiusor from iOS 13 and aboveby setting the layer its maskedCornerproperty.

您可以通过增加图层的cornerRadius或从iOS 13 及更高版本通过设置图层的maskedCorner属性来更改UISegmentedControlcornerRadius 。

This example removes the default cornerRadiusand straightens the backgroundImage:

此示例删除默认的cornerRadius并拉直backgroundImage

if #available(iOS 13.0, *) {
    segmentedControl.layer.maskedCorners = .init()
} else {
    segmentedControl.layer.cornerRadius = 0
}

Source: https://developer.apple.com/documentation/quartzcore/calayer/2877488-maskedcorners

来源:https: //developer.apple.com/documentation/quartzcore/calayer/2877488-maskedcorners

回答by Merricat

iOS 13 - Getting close...

iOS 13 - 接近...

ios 13 segmented control

ios 13分段控制

The problem that I'm having is the animation. Somehow the imageView's frame gets resized.

我遇到的问题是动画。不知何故,imageView 的框架被调整大小。

Code:

代码

class CustomSegmentedControl: UISegmentedControl {

    override func layoutSubviews(){

        super.layoutSubviews()

        //corner radius
        let cornerRadius = bounds.height/2
        let maskedCorners: CACornerMask = [.layerMinXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMinYCorner, .layerMaxXMaxYCorner]
        //background
        clipsToBounds = true
        layer.cornerRadius = cornerRadius
        layer.maskedCorners = maskedCorners
        //foreground
        let foregroundIndex = numberOfSegments
        if subviews.indices.contains(foregroundIndex), let foregroundImageView = subviews[foregroundIndex] as? UIImageView
        {
            foregroundImageView.bounds = foregroundImageView.bounds.insetBy(dx: 5, dy: 5)
            foregroundImageView.image = UIImage()
            foregroundImageView.highlightedImage = UIImage()
            foregroundImageView.backgroundColor = UIColor.darkGray
            foregroundImageView.clipsToBounds = true
            foregroundImageView.layer.masksToBounds = true

            foregroundImageView.layer.cornerRadius = 14
            foregroundImageView.layer.maskedCorners = maskedCorners
        }
    }
}