0

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 :

4
  • as you are using sparteLine is global view variable so its overridding object value with last initiated object and thats why its taking last object and set only last object frame. Commented Feb 15, 2017 at 9:19
  • Can you show your disComment view code ? how you are adding that view ? Commented Feb 15, 2017 at 9:25
  • I have already check that gist but there is no declaration of that view only seperatorView is there. Commented Feb 15, 2017 at 9:28
  • @CodeChanger update that one please check please Commented Feb 15, 2017 at 9:30

3 Answers 3

4

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)
Sign up to request clarification or add additional context in comments.

3 Comments

No need for a function - he could use a var as long as it's a getter, rather than an execute once closure (as he currently has it).
@Grimxn could you give your answer
@DanW, Nazmul - Dan's point is correct, but I suspect that so are the other commentators who point out that the frames and/or constraints need set properly too...
0

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.

2 Comments

let aSparteLine = sparteLine self.addSubview(aSparteLine) . it is tableview cell
Yes so for that you need to post full code otherwise we can not guide you by partially code gist.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.