Nazmul Hasan
Nazmul Hasan

Reputation: 10610

How can i reuse UIView Swift?

i want to achieve like this image .

enter image description here

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

thank you . if your need any information then let me know please :

Upvotes: 0

Views: 2025

Answers (3)

Grimxn
Grimxn

Reputation: 22507

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. There are two ways to fix that:

either make the var a getter rather than an execute-once closure:

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.

2) You are not setting the frames and/or constraints of the UIViews, so they will default to no size, and overlapping.

Try this in Playground (other views removed for clarity):

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

enter image description here

Upvotes: 0

CodeChanger
CodeChanger

Reputation: 8381

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.

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.

Upvotes: 0

Dan W
Dan W

Reputation: 74

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

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

addSubview(separator)

Upvotes: 4

Related Questions