wz3*_*366 37 cocoa-touch uisegmentedcontrol uikit uiview ios
是否可以更改UISegmentedControl的转角半径?我尝试过以下方法,用于改变UIView的角半径.
self.segmentedControl.layer.cornerRadius = 15.0;
self.segmentedControl.layer.masksToBounds = YES;
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为你可以看到它只切断了UISegmentedControl的角落.
谢谢!
xi.*_*lin 99
这应该工作:
self.segmentedControl.layer.cornerRadius = 15.0;
self.segmentedControl.layer.borderColor = [UIColor whiteColor].CGColor;
self.segmentedControl.layer.borderWidth = 1.0f;
self.segmentedControl.layer.masksToBounds = YES;
Run Code Online (Sandbox Code Playgroud)
您需要在设置cornerRadius后指定边框.
Yak*_*sky 34
在UIView中嵌入UISegmentedControl并为UIView设置角半径.
Objective-C的
outerView.layer.cornerRadius = CGRectGetHeight(outerView.bounds) / 2;
outerView.layer.borderColor = [UIColor blueColor].CGColor;
outerView.layer.borderWidth = 1;
Run Code Online (Sandbox Code Playgroud)
迅速
outerView.layer.cornerRadius = CGRectGetHeight(outerView.bounds) / 2
outerView.layer.borderColor = UIColor.blueColor().CGColor
outerView.layer.borderWidth = 1
Run Code Online (Sandbox Code Playgroud)
Rai*_*idi 17
我决定再试一次,我终于让它完美地工作了!!有一行代码可以解决所有问题!查看代码
代码:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
这个想法是 Apple 使用 UIImageView 和它自己的方形图像和所选移动段的色调。我们想要做的是覆盖它的图像,这样我们就可以控制颜色、圆角半径等。在那之后,我们想要删除 Apple 的 3 个默认动画之一(有问题的一个使片段在触摸时放大 - - 我曾经foregroundImageView.layer.animationKeys()
找出影响移动部分的动画)
mat*_*att 12
分段控制不会改变它的角落,所以它继续以自己的方式绘制角落,然后你将其切断.您不负责分段控件如何绘制其边界形状.如果你真的不喜欢它的绘制方式,你将不得不从头开始设计自己的替代控件.你可以合理地接近你想要做的事情是设置分段控件的背景图像.
您可以通过增加图层的cornerRadius或从iOS 13 及更高版本通过设置图层的maskedCorner属性来更改UISegmentedControl cornerRadius 。
此示例删除默认的cornerRadius并拉直backgroundImage:
if #available(iOS 13.0, *) {
segmentedControl.layer.maskedCorners = .init()
} else {
segmentedControl.layer.cornerRadius = 0
}
Run Code Online (Sandbox Code Playgroud)
来源:https : //developer.apple.com/documentation/quartzcore/calayer/2877488-maskedcorners
iOS13有一些变化。因此,您必须从内部设置 borderRadius layoutSubviews
:
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = 2
}
Run Code Online (Sandbox Code Playgroud)
更新以兼容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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
30230 次 |
最近记录: |