alexander alexander - 5 months ago 24
iOS Question

How to Dynamically add a View using Auto Layout

I have this view(pink) in scrollview :

enter image description here

I want this :

when I tap on 'add' button 'new element' button shows and when I re-tap on 'add' button , 'new element ' button hides but I want also this :

when 'new element' button is visible the pink view's height is like in the photo , but when 'new element ' button is not visible the pink view is to decrease the height , but I don't be able to set this situation by auto layout.

Can you help me?


You can try something like this: new element button have boolean "isHidden" in case true or false just set your pink view height (pink.frame.size.height),if is hidden button decrease the height if is not hidden increase the height

if newElementButton.isHidden { pinkView.frame.size.height = 50 }
 else { pinkView.frame.size.height = 100 }