Swift上的图层圆角

时间:2019-01-08 00:57:06

标签: swift cornerradius

我试图用layer.cornerRadius来获得圆角,以进行分段控制,但是它实际上对我不起作用,我想根据框架来获取cornerRadius

这是我的代码

func setupView(){
    layer.masksToBounds = true

    // here my corner radius 
    layer.cornerRadius = frame.height / 2.0
    layer.borderColor = UIColor.walkthroughOrangeAccent.cgColor
    layer.borderWidth = 2

    backgroundColor = UIColor.clear

    setupImageView()

    addIndividualItemConstraints(items: imageViewList, mainView: self, padding: 0)

    insertSubview(thumbView, at: 0)

}

1 个答案:

答案 0 :(得分:0)

我怀疑由于布局限制,您没有看到拐角半径。尝试对分段控件进行子类化,并在layoutSubviews中设置拐角半径:

override func layoutSubviews() {
    super.layoutSubviews()
    layer.cornerRadius = frame.height / 2.0
}

或者,如果您在视图控制器中:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    segmentedControl.layer.cornerRadius = segmentedControl.frame.width / 2.0
}