Ios 将多个独立项目集中在swift中
正如你所见,两个是一个标签,两个图像和两个标签,都是独立的。我需要把所有这些东西放在中心 我以编程的方式做这项工作。请建议 尝试使用way,但失败:Ios 将多个独立项目集中在swift中,ios,swift,uiview,Ios,Swift,Uiview,正如你所见,两个是一个标签,两个图像和两个标签,都是独立的。我需要把所有这些东西放在中心 我以编程的方式做这项工作。请建议 尝试使用way,但失败: func setupViews(){ view.addSubview(orderLocation) orderLocation.addSubview(ownOrderTagImage) orderLocation.addSubview(ownOrderTagLabel) orderLocation.add
func setupViews(){
view.addSubview(orderLocation)
orderLocation.addSubview(ownOrderTagImage)
orderLocation.addSubview(ownOrderTagLabel)
orderLocation.addSubview(ownOrderLocationImage)
orderLocation.addSubview(ownOrderLocationLabel)
//Change the width and height accordingly
view.addConstraintsWithFormat(format: "H:|[v0]|", views: orderLocation)
view.addConstraintsWithFormat(format: "V:|-50-[v0(20)]|", views: orderLocation)
orderLocation.addConstraintsWithFormat(format: "H:|[v0(18)][v1(70)]-20-[v2(18)][v3(80)]|", views: ownOrderTagImage, ownOrderTagLabel,ownOrderLocationImage,ownOrderLocationLabel)
orderLocation.addConstraintsWithFormat(format: "V:|[v0(20)]", views: ownOrderTagImage)
orderLocation.addConstraintsWithFormat(format: "V:|[v0(20)]", views: ownOrderTagLabel)
orderLocation.addConstraintsWithFormat(format: "V:[v0(20)]|", views: ownOrderLocationImage)
orderLocation.addConstraintsWithFormat(format: "V:[v0(20)]|", views: ownOrderLocationLabel)
}
addConstraintsWithFormat
并非swift的固有组成部分,而是可视格式语言的扩展
我相信您正在使用以下代码作为扩展
extension UIView {
func addConstraintsWithFormat(_ format: String, views : UIView...) {
var viewsDictionary = [String: UIView]()
for(index, view) in views.enumerated(){
let key = "v\(index)"
viewsDictionary[key] = view
view.translatesAutoresizingMaskIntoConstraints = false
}
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
}
你必须记住在将来的问题中包含类似的信息
无论如何,您缺少要将视图添加到的视图。
在您的情况下,应该将ImageView作为superview
首先创建ImageView对象,在您的情况下,它将成为该条
let imageView: UIImageView = {
let imgV = UIImageView()
//put the name of the image you are using in the quotes
imgV.image = UIImage(named: "")
imgV.layer.masksToBounds = true
imgV.contentMode = .scaleAspectFill
return imgV
}()
确保使用的照片格式正确,并将barView添加为子视图及其约束的超级视图
试试看
//here you add the view
view.addSubview(imageView)
imageView.addSubview(ownOrderTagImage)
imageView.addSubview(ownOrderTagLabel)
imageView.addSubview(ownOrderLocationImage)
imageView.addSubview(ownOrderLocationLabel)
//Change the width and height accordingly
view.addConstraintsWithFormat(format: "H:|[v0(100)]|", views: imageView)
view.addConstraintsWithFormat(format: "V:|[v0(10)]|", views: imageView)
imageView.addConstraintsWithFormat(format: "H:|[v0][v1]|", views: ownOrderTagImage, ownOrderTagLabel)
imageView.addConstraintsWithFormat(format: "H:|[v0][v1]|", views: ownOrderLocationImage, ownOrderLocationLabel)
imageView.addConstraintsWithFormat(format: "V:|[v0(44)]", views: ownOrderTagImage)
imageView.addConstraintsWithFormat(format: "V:|[v0(44)]", views: ownOrderTagLabel)
imageView.addConstraintsWithFormat(format: "V:[v0(30)]|", views: ownOrderLocationImage)
imageView.addConstraintsWithFormat(format: "V:[v0(30)]|", views: ownOrderLocationLabel)
您有一个由四个视图组成的组,要将其排列成一行,并且要将该组作为一个整体居中放置在superview中 组中心没有可用于约束的定位点。所有可用的定位点都位于各个子视图的中心或边缘 不能将“从最左侧项目到超级视图的距离”约束为等于“从最右侧项目到超级视图的距离”,因为该约束将涉及四个定位点,而自动布局约束只能涉及两个定位点 一种解决方案是添加一个紧密包围视图组的辅助视图。然后可以将辅助视图的中心定位约束到superview的中心定位 最简单的方法是将视图组放在堆栈视图中,并将堆栈视图居中。如果使用堆栈视图,还需要一个“间隔”视图来将“标记”视图与“位置”视图分开 以下是我的测试结果: 这是我的测试场:
import UIKit
import PlaygroundSupport
let rootView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 100))
rootView.backgroundColor = #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1)
PlaygroundPage.current.liveView = rootView
let tagImageView = UIImageView(image: UIImage(named: "Toggle"))
tagImageView.contentMode = .scaleAspectFit
let tagLabel = UILabel()
tagLabel.text = "#YUIO9N"
let locationImageView = UIImageView(image: UIImage(named: "Gear"))
locationImageView.contentMode = .scaleAspectFit
let locationLabel = UILabel()
locationLabel.text = "Garmany"
let spacer = UIView()
let rowView = UIStackView(arrangedSubviews: [tagImageView, tagLabel, spacer, locationImageView, locationLabel])
rowView.axis = .horizontal
rowView.translatesAutoresizingMaskIntoConstraints = false
rootView.addSubview(rowView)
NSLayoutConstraint.activate([
tagImageView.widthAnchor.constraint(equalToConstant: 18),
spacer.widthAnchor.constraint(equalToConstant: 20),
locationImageView.widthAnchor.constraint(equalToConstant: 18),
rowView.heightAnchor.constraint(equalToConstant: 20),
rowView.centerXAnchor.constraint(equalTo: rootView.centerXAnchor),
rowView.topAnchor.constraint(equalTo: rootView.topAnchor, constant: 20)])
你说失败是什么意思?你能用你发布的代码显示你的结果的屏幕截图吗?同时,显示一张你希望它看起来如何的图像。@DonMag我添加了一张图像,你能检查一下吗。我怎样才能做到。请帮忙me@Nef10我添加了输出截图结果是什么第一个截图期望的外观?或者你想要一个标签在另一个上面@Cristainlik有一个最后的评论。我强烈建议您考虑使用自动布局方法,如NSLayoutConstraints。视觉格式语言只能走这么远。如果您正在构建更详细、更复杂的布局,那么我认为NSLayoutConstraints或NSAnchors是应该采用的方式。。你能查一下我的最新答案吗非常感谢。。您能帮我解决这个问题吗:swift编译器错误UIStackView它仅适用于ios 9如果我设置了部署目标9,它工作正常,但部署目标7不工作。建议请使用您喜爱的搜索引擎查看
UIStackView
后端口,或者使用普通的UIView
手动设置约束。我似乎可以从ios 9获得UISackView。但产品目标是OS7。我能做什么请帮帮我