![](/img/trans.png)
[英]iOS 13 - UISegmentedControl - How do you change the corner radius of the selectedSegment?
[英]How to change the corner radius of UISegmentedControl?
是否可以更改 UISegmentedControl 的角半徑? 我嘗試了以下方法來更改 UIView 的角半徑。
self.segmentedControl.layer.cornerRadius = 15.0;
self.segmentedControl.layer.masksToBounds = YES;
這不起作用,因為您可以看到它只切斷了 UISegmentedControl 的角落。
謝謝!
這應該有效:
self.segmentedControl.layer.cornerRadius = 15.0;
self.segmentedControl.layer.borderColor = [UIColor whiteColor].CGColor;
self.segmentedControl.layer.borderWidth = 1.0f;
self.segmentedControl.layer.masksToBounds = YES;
設置cornerRadius后需要指定邊框。
在 UIView 中嵌入 UISegmentedControl 並為UIView設置圓角半徑。
目標-C
outerView.layer.cornerRadius = CGRectGetHeight(outerView.bounds) / 2;
outerView.layer.borderColor = [UIColor blueColor].CGColor;
outerView.layer.borderWidth = 1;
迅速
outerView.layer.cornerRadius = CGRectGetHeight(outerView.bounds) / 2
outerView.layer.borderColor = UIColor.blueColor().CGColor
outerView.layer.borderWidth = 1
我決定再試一次,我終於讓它完美地工作了!! 有一行代碼可以解決所有問題! 查看代碼
代碼:
class CustomSegmentedControl: UISegmentedControl{
private let segmentInset: CGFloat = 5 //your inset amount
private let segmentImage: UIImage? = UIImage(color: UIColor.red) //your color
override func layoutSubviews(){
super.layoutSubviews()
//background
layer.cornerRadius = bounds.height/2
//foreground
let foregroundIndex = numberOfSegments
if subviews.indices.contains(foregroundIndex), let foregroundImageView = subviews[foregroundIndex] as? UIImageView
{
foregroundImageView.bounds = foregroundImageView.bounds.insetBy(dx: segmentInset, dy: segmentInset)
foregroundImageView.image = segmentImage //substitute with our own colored image
foregroundImageView.layer.removeAnimation(forKey: "SelectionBounds") //this removes the weird scaling animation!
foregroundImageView.layer.masksToBounds = true
foregroundImageView.layer.cornerRadius = foregroundImageView.bounds.height/2
}
}
}
extension UIImage{
//creates a UIImage given a UIColor
public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
let rect = CGRect(origin: .zero, size: size)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
color.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
guard let cgImage = image?.cgImage else { return nil }
self.init(cgImage: cgImage)
}
}
這個想法是 Apple 使用 UIImageView 和它自己的方形圖像和所選移動段的色調。 我們想要做的是覆蓋它的圖像,這樣我們就可以控制顏色、圓角半徑等。在那之后,我們想要刪除 Apple 的 3 個默認動畫之一(有問題的一個使片段在觸摸時放大- - 我使用了foregroundImageView.layer.animationKeys()
來找出影響移動片段的動畫)
分段控件不會改變它繪制角的方式,因此它會繼續以自己的方式繪制角,然后您將它們切斷。 您不負責分段控件如何繪制其邊界形狀。 如果您真的不喜歡它的繪制方式,則必須從頭開始設計自己的替代控件。 您可以合法地嘗試做的最接近的事情是設置分段控件的背景圖像。
您可以通過增加圖層的cornerRadius或從iOS 13 及更高版本通過設置圖層的maskedCorner屬性來更改UISegmentedControlcornerRadius 。
此示例刪除默認的cornerRadius並拉直backgroundImage :
if #available(iOS 13.0, *) {
segmentedControl.layer.maskedCorners = .init()
} else {
segmentedControl.layer.cornerRadius = 0
}
來源: https : //developer.apple.com/documentation/quartzcore/calayer/2877488-maskedcorners
針對 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
您的結果是因為其他(自定義繪圖?)控制邊框而不是圖層。 幸運的是,該圖層設置似乎具有優先權。
如果你知道你需要什么邊框顏色,你可以添加(示例):
self.segmentedControl.layer.borderColor = [UIColor whiteColor].CGColor;
self.segmentedControl.layer.borderWidth = 1.0;
以前的解決方案對我不起作用。 我的解決辦法是:
要將UISegmentedControl
嵌入到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
iOS13 有一些變化。 因此,您必須從layoutSubviews
設置 borderRadius :
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = 2
}
這是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
使用以下代碼:
segmentContrl.layer.borderColor=*anycolor*.CGColor;
segmentContrl.layer.cornerRadius = 0.0;
segmentContrl.layer.borderWidth = 1.5f;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.