0

I have a view in which i am creating another view programmatically on add button through xib. I am able to create multiple views on tapping add more button and remove is also working if I remove last view but the problem occurs with middle views due to missing constraints view not updating correctly bellow are images how it is looking

At start view Look like this

enter image description here

After Adding more view

enter image description here

After removing middle view

enter image description here

Delete button code

@IBAction func deletebnt(_ sender: UIButton) {
        let view = self.superview
        let index =  view?.subviews.index(of:self)!
        delegate.txtcheck(text: countstr)
         self.view.removeFromSuperview()
    }

Add button Code

@IBAction func addMoreBnt(_ sender: UIButton) {
      for constraint in addSuperview.constraints {
            if constraint.firstAttribute == NSLayoutAttribute.height
            {
                constraint.constant +=  45
                space = constraint.constant
            }
        }
        let newView : AvalabileTimeView = AvalabileTimeView()
        newView.frame = CGRect(x: self.addsubView.frame.origin.x, y: 70, width: addsubView.frame.size.width, height:addsubView.frame.size.height)
        newView.delegate = self as AvalabileTimeDelegate
        addSuperview.addSubview(newView)
        let index = addSuperview.subviews.index(of: newView)!        
        newView.translatesAutoresizingMaskIntoConstraints = false
        let heightConstraint = newView.widthAnchor.constraint(equalToConstant:addsubView.frame.size.width )
        let widthConstaint = newView.heightAnchor.constraint(equalToConstant:31 )
        let topConstraint = newView.topAnchor.constraint(equalTo: addSuperview.topAnchor, constant: space - 31)  NSLayoutConstraint.activate([heightConstraint,topConstraint,widthConstaint])

    }

delegate to change height of superview

func txtcheck(text: String!) {
        print(text)
        for constraint in addSuperview.constraints {
            if constraint.firstAttribute == NSLayoutAttribute.height
            {
                constraint.constant -=  45
                // Here I have to set constraint for bottom view and topview of deleted view but I don't know how to do
            }
        }
    }

Here is link to demo project https://github.com/logictrix/addFieldDemo

mfaani
  • 33,269
  • 19
  • 164
  • 293
Varun Naharia
  • 5,318
  • 10
  • 50
  • 84
  • Use UITabelView. You can achieve This Type of Action very easily. – Ujesh Jun 15 '17 at 12:37
  • @Ujesh Yes I have to use other option if I failed to fix this, right now I am trying to fix this. – Varun Naharia Jun 15 '17 at 12:41
  • This can be of course fixed, but it's better to use a TableView, It is understandable if this is an assignment where you are unable to use TableViews. Otherwise always use the best tools Apple provides you for the job. – Aju Antony Jun 15 '17 at 12:58
  • @CoderFrom94 This not assignment but if I use tableview I have to design & code for the whole ViewController from start that I don't want to do if I can quick fix this with some help – Varun Naharia Jun 15 '17 at 13:03
  • 1
    TableView might have been a good idea for the *full* interface, but trying to add a TableView for just that section would be really tough to make work (vertical scrolling table view inside a vertical scrolling scroll view). – DonMag Jun 15 '17 at 13:17
  • @VarunNaharia Add code : self.view.layoutIfNeeded() in deleteButton Method as last line of that method. Also instead of using UIView && addd it every time , tableview is better option. please try this using tableview you problem will solve...!!! – Ashok Londhe Jun 15 '17 at 13:47

1 Answers1

3

Instead of adding each new AvalabileTimeView with its own constraints, use a UIStackView - you can remove almost all of your existing code for adding / removing the new views.

Look at .addArrangedSubview() and .removeArrangedSubview()

Here is some sample code... you'll need to add a UIStackView in Interface Builder, connect it to the Outlet, and adjust the constraints, but that's about all:

// in ViewController.swift

@IBOutlet weak var availableTimeStackView: UIStackView!

@IBAction func addMoreBnt(_ sender: UIButton) {

    // instantiate a new AvalabileTimeView
    let newView : AvalabileTimeView = AvalabileTimeView()

    // set its delegate to self
    newView.delegate = self as AvalabileTimeDelegate

    // add it to the Stack View
    availableTimeStackView.addArrangedSubview(newView)

    // standard for auto-layout
    newView.translatesAutoresizingMaskIntoConstraints = false

    // only constraint needed is Height (width and vertical spacing handled by the Stack View)
    newView.heightAnchor.constraint(equalToConstant: 31).isActive = true

}

// new delegate func
func removeMe(_ view: AvalabileTimeView) {

    // remove the AvalabileTimeView from the Stack View
    availableTimeStackView.removeArrangedSubview(view)

}

// in AvalabileTimeView.swift

protocol AvalabileTimeDelegate{
    // don't need this anymore
    func txtcheck(text: String!)

    // new delegate func
    func removeMe(_ view: AvalabileTimeView)
}

@IBAction func deletebnt(_ sender: UIButton) {
    delegate.removeMe(self)
}
DonMag
  • 69,424
  • 5
  • 50
  • 86
  • Thanks for remembering me about stack view I totally forgot about that It might solve my problem I'll check and let you about that – Varun Naharia Jun 15 '17 at 13:27