【问题标题】:How can I add same uiview multiples times at different positions to a UIViewController?如何在不同位置将相同的 uiview 多次添加到 UIViewController?
【发布时间】:2015-08-15 19:23:25
【问题描述】:

我正在努力实现这一目标。

到目前为止,我创建了自己的自定义视图,它是 UIView 类的子类:

 class MyConnections: UIView {

override init(frame: CGRect) {
    super.init(frame: frame)
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
    // Drawing code
    let context = UIGraphicsGetCurrentContext()
    CGContextSetLineWidth(context, 1)
    CGContextSetStrokeColorWithColor(context, UIColor.blackColor().CGColor)
    let circle = CGRectMake(5, 60, 80, 80)
    CGContextAddEllipseInRect(context, circle)
    CGContextStrokePath(context)
    CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
    CGContextFillEllipseInRect(context, circle)

}

}

这是我的视图控制器,我将上面的视图添加为子视图:

let profile = MyConnections()

override func viewDidLoad() {
    super.viewDidLoad()

    profile.backgroundColor = UIColor.clearColor()
    view.addSubview(profile)
    self.profile.setTranslatesAutoresizingMaskIntoConstraints(false)

    //constraints for the location button
    let horizontalConstraint = NSLayoutConstraint(item: self.profile, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 10)
    let verticalConstraint = NSLayoutConstraint(item: self.profile
        , attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 20)
    let widthConstraint = NSLayoutConstraint(item: self.profile, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 150)
    let heightConstraint = NSLayoutConstraint(item: self.profile, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 150)

    self.view.addConstraints([verticalConstraint, horizontalConstraint, widthConstraint, heightConstraint])
    // Do any additional setup after loading the view.
}

上面的所有代码都给了我一个圆圈。现在我想在图像中看到的不同位置多次重复同一个圆圈。我可以创建 uiview 的多个实例,将它们添加为子视图,但每次我都必须为它定义我不想做的新约束。

谁能帮助我并给我一个有效的答案?

【问题讨论】:

  • UICollectionView 可能更适合这样的事情。
  • UICollectionView 怎么样?
  • @rmaddy 我想在不使用情节提要的情况下创建一个集合视图。我的整个项目使用 .xib 文件。我找不到任何显示在没有情节提要的情况下创建集合视图的教程。你能在这里分享一些链接吗?

标签: ios swift uiview uiviewcontroller


【解决方案1】:

您应该知道 UIView 可以有一个父视图/父视图。如果您将它作为子视图添加到不同的位置(使用 addSubview 方法),它将从第一个位置删除并作为子视图添加到新位置。 在您的情况下,要添加更多子视图,您必须创建更多 UIView 对象而不使用单个全局 UIView。 如果布局重复,则 UITableView / UICollectionView 是更好的选择。

【讨论】:

  • 感谢大家的回答,但我想在不使用情节提要的情况下创建一个集合视图。我的整个项目使用 .xib 文件。我找不到任何显示在没有情节提要的情况下创建集合视图的教程。你能在这里分享一些链接吗?
  • 我想已经很晚了,但是对于其他还没有遇到过这个的人,您可以通过编程方式制作一个集合视图或几乎任何其他视图。对于集合视图,您只需像上面的 uiview 一样以编程方式声明视图,请记住,如果您使用流布局,您还需要以编程方式声明布局。
【解决方案2】:

您的要求和 UI 符合 UICollectionView 我认为您应该使用 UICollectionView 并且可以创建带有圆形图像和徽章视图的自定义 UICollectionViewCell,并且它们添加了 dataSource 和委托方法。这不仅有助于创建 UI,还可以通过重用单元格来提高应用程序的性能 Here 是一个很好的关于 UICollectionView 的教程

【讨论】:

    【解决方案3】:

    这是一个以编程方式创建集合视图的简化视图: 以编程方式制作集合视图和布局,就像您编写的任何其他视图一样,并将其添加为子视图,如下所示:

    lazy var myCollectionView : UICollectionView = {
        let layout = YourFlowLayout()
        layout.scrollDirection = self.direction;
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
    
        let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        cv.dataSource = self
        cv.delegate = self
        cv.isPagingEnabled = true
        cv.backgroundColor = UIColor.clear
        cv.showsHorizontalScrollIndicator = false
        cv.showsVerticalScrollIndicator = false
        cv.allowsMultipleSelection = false
        return cv
    }()
    

    你的流程布局可能是这样的:

    mport UIKit
    
    class Yourflowlayout: UICollectionViewFlowLayout {
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    
        return super.layoutAttributesForElements(in: rect)?.map {
            attrs in
            let attrscp = attrs.copy() as! UICollectionViewLayoutAttributes
            self.applyLayoutAttributes(attributes: attrscp)
            return attrscp
        }
    
    }
    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        if let attrs = super.layoutAttributesForItem(at: indexPath as IndexPath) {
            let attrscp = attrs.copy() as! UICollectionViewLayoutAttributes
            self.applyLayoutAttributes(attributes: attrscp)
            return attrscp
        }
        return nil
    }
    
    
    func applyLayoutAttributes(attributes : UICollectionViewLayoutAttributes) {
        if attributes.representedElementKind != nil {
            return
        }
    
        if let collectionView = self.collectionView {
            let stride = (self.scrollDirection == .horizontal) ? collectionView.frame.size.width : collectionView.frame.size.height
            let offset = CGFloat(attributes.indexPath.section) * stride
            var xCellOffset : CGFloat = CGFloat(attributes.indexPath.item) * self.itemSize.width
            var yCellOffset : CGFloat = CGFloat(attributes.indexPath.item) * self.itemSize.height
            if(self.scrollDirection == .horizontal) {
                xCellOffset += offset;
            } else {
                yCellOffset += offset
            }
            attributes.frame = CGRect(x: xCellOffset, y: yCellOffset, width: self.itemSize.width, height: self.itemSize.height)
        }
    }
    

    }

    您可以在其他类中将 collectionView 添加为子视图,请确保您拥有 myCollectionView.translatesAutoresizingMaskIntoConstraints = false 以便应用您的约束并且您实际上可以看到集合视图,当然还可以添加您的约束或给它一个框架。

    希望对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-06
      相关资源
      最近更新 更多