[英]How to prevent from clipping the subviews when I set the cornerRadius of a UIView?
我正在嘗試設置UIButton的子類視圖的cornerRadius,圓角以正確的方式顯示,但是當我嘗試在其上添加subView(花形圖標)時,該子視圖似乎像在圖片上一樣被剪切了。在下面的右側 ,這不是我所期望的。 我嘗試做出正確的外觀,如左側的圖片所示,不剪切圖標。 我使用的代碼:
button.layer.cornerRadius = button.frame.width / 2;
button.layer.masksToBounds = Yes;
希望有人可以幫助我了解如何防止裁剪。
謝謝!
然后,您不應該將疊加層添加為子視圖。 如果將clipsToBounds
設置為YES
則子視圖將被裁剪。
而是將其添加為同級,如下所示:
- container view
- image view (clips)
- overlay view
如果您使用上述代碼對按鈕進行四舍五入,那么您的按鈕肯定會從角落切掉,因此,如果您只想從3個角落切掉,請執行以下操作:
#import <QuartzCore/CoreAnimation.h>
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:button.bounds
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft
cornerRadii:CGSizeMake(7.0, 7.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = button.bounds;
maskLayer.path = maskPath.CGPath;
button.layer.mask = maskLayer;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.