大佬教程收集整理的这篇文章主要介绍了iOS – 在堆栈视图中以编程方式添加垂直线,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所需的完成将是这样的图像:
我可以添加标签,所有标签都有所需的间距;我可以添加水平线,但我无法弄清楚如何在中间添加这些分隔符垂直线.
我想这样做:
let stackView = UIStackView(arrangedSubviews: [label1,verticalLine,label2,label3])
任何提示?
谢谢
>设置背景颜色.
>将宽度限制为1(因此您得到一条线,而不是一个矩形).
>限制其高度(因此它不会被拉伸到堆栈视图的整个高度).
因此,一次一个地添加标签到堆栈视图,并在将每个标签添加到堆栈视图之前执行类似的操作:
if stackView.arrangedSubviews.count > 0 { let separator = UIView() separator.widthAnchor.consTraint(equalToConstant: 1).isActive = true separator.BACkgroundColor = .black stackView.addArrangedSubview(separator) separator.heightAnchor.consTraint(equalTo: stackView.heightAnchor,multiplier: 0.6).isActive = true }
请注意,您不希望垂直线与标签的宽度相同,因此您不能将堆栈视图的分布属性设置为fillEqually.相反,如果您希望所有标签具有相等的宽度,则必须自己在标签之间创建宽度约束.例如,在添加每个新标签后,执行以下操作:
if let firstLabel = stackView.arrangedSubviews.first as? UILabel { label.widthAnchor.consTraint(equalTo: firstLabel.widthAnchor).isActive = true }
结果:
完整的游乐场代码(由Federico Zanetello更新为Swift 4.1):
import UIKit import PlaygroundSupport extension UIFont { var withsmallCaps: UIFont { let feature: [UIFontDescriptor.FeatureKey: Any] = [ UIFontDescriptor.FeatureKey.featurEIDentifier: kLowerCaseType,UIFontDescriptor.FeatureKey.typEIDentifier: kLowerCasesmallCapsSELEctor] let attributes: [UIFontDescriptor.Attributename: Any] = [UIFontDescriptor.Attributename.featureSetTings: [feature]] let descriptor = self.fontDescriptor.addingAttributes(attributes) return UIFont(descriptor: descriptor,size: pointSizE) } } let rootView = UIView(frame: CGRect(x: 0,y: 0,width: 320,height: 44)) rootView.BACkgroundColor = .white PlaygroundPage.current.liveView = rootView let stackView = UIStackView() stackView.axis = .horizontal stackView.alignment = .center stackView.frame = rootView.bounds rootView.addSubview(stackView) typealias Item = (name: String,value: int) let items: [Item] = [ Item(name: "posts",value: 135),Item(name: "followers",value: 6347),Item(name: "following",value: 328),] let valueStyle: [NSAttributedStringKey: Any] = [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 12).withsmallCaps] let nameStyle: [NSAttributedStringKey: Any] = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 12).withsmallCaps,NSAttributedStringKey.foregroundColor: UIColor.darkGray] let valueFormatter = numberFormatter() valueFormatter.numberStyle = .decimal for item in items { if stackView.arrangedSubviews.count > 0 { let separator = UIView() separator.widthAnchor.consTraint(equalToConstant: 1).isActive = true separator.BACkgroundColor = .black stackView.addArrangedSubview(separator) separator.heightAnchor.consTraint(equalTo: stackView.heightAnchor,multiplier: 0.4).isActive = true } let richText = NSMutableAttributedString() let valueString = valueFormatter.String(for: item.value)! richText.append(NSAttributedString(String: valueString,attributes: valueStylE)) richText.append(NSAttributedString(String: "\n" + item.name,attributes: nameStylE)) let label = UILabel() label.attributedText = richText label.textAlignment = .center label.numberOfLines = 0 stackView.addArrangedSubview(label) if let firstLabel = stackView.arrangedSubviews.first as? UILabel { label.widthAnchor.consTraint(equalTo: firstLabel.widthAnchor).isActive = true } } UIGraphicsBeginImageContextWithOptions(rootView.bounds.size,true,1) rootView.drawHierarchy(in: rootView.bounds,afterScreenupdates: truE) let image = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() let png = UIImagePNGRepresentation(imagE)! let path = NstemporaryDirectory() + "/image.png" Swift.print(path) try! png.write(to: URL(fileURLWithPath: path))
以上是大佬教程为你收集整理的iOS – 在堆栈视图中以编程方式添加垂直线全部内容,希望文章能够帮你解决iOS – 在堆栈视图中以编程方式添加垂直线所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。