ios - Swift:调用函数时创建 UI

标签 ios swift view

我这里有这些 View 之一:

let placeContainerView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor.whiteColor()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.layer.cornerRadius = 7
    view.layer.masksToBounds = true

    return view
}()

每次调用函数“createNewView()”时,我都希望在它旁边创建一个。

    let showFullPlaceContainerView = UITapGestureRecognizer(target: self, action: #selector(showFullPlaceContainerViewFunction))
    placeContainerView.addGestureRecognizer(showFullPlaceContainerView)

我希望每个 placeContainerView 都能响应。

所以我想要生成一个 View ,我会为它赋予特定的值。然后当我调用函数 createNewView() 时,我会在它旁边得到一个新 View ,除了我输入的任何新值外,它完全相同。

如果您有任何想法,请告诉我!

谢谢

编辑:

下面的代码演示了我想如何每次设置 placeContainerView,但我需要显示它们,以便 placeContainerView.topAnchor() 每次都不同。如果将它保存在自己的类中,它究竟如何工作并且不知道创建了多少次?

此外,由于 placeContainerView 包含 placeLabel 和 placeImageView,是否也必须在新的 PlaceContainerViewClass 中生成它们?

func setupPlaceContainerView() {
    placeContainerView.leftAnchor.constraintEqualToAnchor(view.leftAnchor, constant: -180).active = true
    placeContainerView.topAnchor.constraintEqualToAnchor(view.topAnchor, constant: 80).active = true
    placeContainerView.widthAnchor.constraintEqualToConstant(227).active = true
    placeContainerView.heightAnchor.constraintEqualToConstant(45).active = true


    placeContainerView.addSubview(placeLabel)
    placeContainerView.addSubview(placeImageLabelSeparator)
    placeContainerView.addSubview(placeImageView)

    placeLabel.leftAnchor.constraintEqualToAnchor(placeContainerView.leftAnchor).active = true
    placeLabel.topAnchor.constraintEqualToAnchor(placeContainerView.topAnchor).active = true
    placeLabel.widthAnchor.constraintEqualToConstant(180).active = true
    placeLabel.heightAnchor.constraintEqualToAnchor(placeContainerView.heightAnchor).active = true

    placeImageLabelSeparator.leftAnchor.constraintEqualToAnchor(placeLabel.rightAnchor).active = true
    placeImageLabelSeparator.topAnchor.constraintEqualToAnchor(placeContainerView.topAnchor).active = true
    placeImageLabelSeparator.widthAnchor.constraintEqualToConstant(2).active = true
    placeImageLabelSeparator.heightAnchor.constraintEqualToAnchor(placeContainerView.heightAnchor).active = true

    placeImageView.leftAnchor.constraintEqualToAnchor(placeImageLabelSeparator.rightAnchor).active = true
    placeImageView.topAnchor.constraintEqualToAnchor(placeContainerView.topAnchor).active = true
    placeImageView.widthAnchor.constraintEqualToConstant(45).active = true
    placeImageView.heightAnchor.constraintEqualToAnchor(placeContainerView.heightAnchor).active = true

最佳答案

如@Paulw11 所述,此任务只需创建一个新类即可完成。

class placeContainerView:UIView {

    var x:Double!
    var y:Bool!
    var z:UILabel!
    var controller:UIViewController!

    //If you want to pass specific values number, you can use convenience init method OR you can use the default init method they give you.
    //previousLabelFrame:CGrect = CGRect() // I defaulted all these values to 0, make them whatevree u need. You would use the default one for the first Label you would make. Then after that, you would pass in the previous one made, to get the frame of it so you can add to the one after that. 
    convenience init(x:Double,y:Bool, z:UILabel, previousLabelFrame:CGRect = CGRect(x: 0, y:0, width:0, height:0), VC:UIViewController) {
        self.x = x
        self.y = y
        self.z = z
        self.controller = VC
        let distance = self.controller.width*0.1 //Whatever u decide here
        //You could just do CGRect(x:previousLabelFrame.maxX+distance, depeding on what you need. 
        self.frame = CGRect(x: previousLabelFrame.minX+distance, y:previousLabelFrame.minY, width: previousLabelFrame.width, height:previousLabelFrame.height)
    }

}

ViewController 内部的用法:

var views:[placeContainerView] = []

let view:placeContainerView = placeContainerView(10, true, UILabel(),views[views.count-1], self)
 self.views.append(view)
//OR if this is the FIRST placeContainerView of the whole app, it will use the default values for the frame.
let view:placeContainerView = placeContainerView(10, true, UILabel(), self)
self.views.append(view)

关于如何使用的一些奇怪的例子。 然后每次他们点击一个按钮,就创建一个新的 placeContainerView

关于ios - Swift:调用函数时创建 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38577157/

相关文章:

swift - .sks 文件中 SKSpriteNode 的子类

android - Eclipse 未找到包 android.view

iOS:在后台获取期间调试 com.apple.NSURLConnectionLoader 线程上的神秘 CFNetwork <redacted> 崩溃

ios - IOS 版本兼容的本地和推送通知

ios - 静态TableViewCells调整高度,填充 View 高度

nhibernate - 如何在 NHibernate 中映射没有标识列的 View ?

android - 在 android 中,将相机预览流式传输到 View 中

objective-c - 托管对象上下文未保存到持久存储

ios - 禁用时分隔符出现在 uitableview 中

ios - 使用 UIRefresh 更新 TableView