简体   繁体   English

如何重用UIView Swift?

[英]How can i reuse UIView Swift?

i want to achieve like this image . 我想实现这样的形象。

在此处输入图片说明

here is my view i want to reuse it for separator line 这是我的观点,我想将其重用于分隔线

var sparteLine : UIView = {
        var view = UIView()
        view.backgroundColor = UIColor.blue // color blue for testing 
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

i just try to do it by this way but it is not working .. it only showing last separator line . 我只是试图通过这种方式做到这一点,但它不起作用..它只显示最后一个分隔线。 first one not showing . 第一个没有显示。 what can i do ?: 我能做什么 ?:

    addSubview(sparteLine)
    addSubview(disComment)
    addSubview(disCommentTextField)
    addSubview(sparteLine)

full source code here : https://gist.github.com/nazmulkp/c0b57185f76fb426634c65eb0476889e 完整的源代码在这里: https : //gist.github.com/nazmulkp/c0b57185f76fb426634c65eb0476889e

thank you . 谢谢 。 if your need any information then let me know please : 如果您需要任何信息,请告诉我:

You're attempting to add the same view as a subview more than once, which is not possible. 您试图多次将同一视图作为子视图添加,这是不可能的。

What you could do, is create a function that creates your separator view, and create one each time you need one. 您可以做的是创建一个创建分隔符视图的函数,并在每次需要时创建一个。

func createSeparatorLine() -> UIView {
    let view = UIView()
    view.backgroundColor = .blue
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}

Each time you need to use it, simply call this function 每次需要使用它时,只需调用此函数

let separator1 = createSeparatorLine()
addSubview(separator1)

EDIT Good point Grimxn 编辑好点Grimxn

var separator: UIView {
    let view = UIView()
    view.backgroundColor = .blue
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}

addSubview(separator)

You need to change code little bit. 您需要一点点更改代码。

as per my observation while you are setting Constrains its taking new object so catch that view object in local variable and use it for setting Constrains . 根据我的观察,当您设置Constrains它采用新对象,因此在局部变量中捕获该视图对象并将其用于设置Constrains

Like this : 像这样 :

 let aSparteLine = sparteLine
 self.view.addSubview(aSparteLine)


//Mark :- sparteLine

        NSLayoutConstraint(item: aSparteLine, attribute: NSLayoutAttribute.top, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: 10).isActive = true

        NSLayoutConstraint(item: aSparteLine, attribute: NSLayoutAttribute.left, relatedBy: .equal, toItem: self.view, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 10).isActive = true

        NSLayoutConstraint(item: aSparteLine, attribute: NSLayoutAttribute.right, relatedBy: .equal, toItem: self.view, attribute: NSLayoutAttribute.right, multiplier: 1, constant: -10).isActive = true

        NSLayoutConstraint(item: aSparteLine, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier:0, constant: 5).isActive = true

Hope this will helps you. 希望这对您有帮助。

There are two problems with you code: 您的代码有两个问题:

1) your definition of sparteLine is an execute-once closure, and so you are trying to add the same instance of UIView as a subview twice, which as @DanW points out, will not work. 1)您对sparteLine的定义是一次执行的关闭,因此您试图两次将相同UIView 实例添加为子视图,如@DanW所指出的那样,它将不起作用。 There are two ways to fix that: 有两种解决方法:

either make the var a getter rather than an execute-once closure: 要么使var成为getter而不是一次执行闭包:

var separator: UIView {
    let view = UIView()
    view.backgroundColor = .blue
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}

or make it a function. 使其功能。 In either case, you will then have two separate instances of UIView. 无论哪种情况,您都将拥有两个单独的UIView实例。

2) You are not setting the frames and/or constraints of the UIView s, so they will default to no size, and overlapping. 2)您没有设置UIView的框架和/或约束,因此它们将默认为无大小且重叠。

Try this in Playground (other views removed for clarity): 在Playground中尝试此操作(为了清楚起见,删除了其他视图):

func sparteLine(_ y: CGFloat, _ colour: UIColor) -> UIView {
    let view = UIView(frame: CGRect(x: 0, y: y, width: 200, height: 100))
    view.backgroundColor = colour // color blue for testing
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}

let mainView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
mainView.addSubview(sparteLine(0, .blue))
mainView.addSubview(sparteLine(100, .red))
mainView

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM