UIBezierPath 在右上角和右下角不起作用 [英] UIBezierPath doesn't work in TopRight corner and BottomRight corner

查看:28
本文介绍了UIBezierPath 在右上角和右下角不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想圆我的右角,但只适用于左角

I want to round my right corners, but only works for Left corners

let path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: [UIRectCorner.TopLeft, UIRectCorner.TopRight], cornerRadii: CGSizeMake(20.0, 20.0))
let maskLayer = CAShapeLayer()
    maskLayer.path = path.CGPath
    view.layer.mask = maskLayer
    view.layer.masksToBounds = true

推荐答案

问题是您在针对当前设备调整视图大小之前获得了视图的边界.它比稍后出现在屏幕上时要大,因此圆角矩形的右侧不在屏幕右侧.

The problem is that you're getting the view's bounds before it's been resized for the current device. It's larger than it will be later when it appears on screen, so the right side of your rounded rect is off the right side of the screen.

您需要稍后创建 path 并设置 maskLayer.path,在调整视图大小之后,这意味着在运行循环的布局阶段期间或之后.因此,要么将视图子类化并在视图的 layoutSubviews 方法中执行,要么在视图控制器的 viewDidLayoutSubviews 方法中执行.

You need to create path and set maskLayer.path later, after the view has been resized, which means during or after the layout phase of the run loop. So either subclass the view and do it in the view's layoutSubviews method, or do it in the view controller's viewDidLayoutSubviews method.

这篇关于UIBezierPath 在右上角和右下角不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆