简体   繁体   中英

How to center align 2 UIViews inside a container (UIView), without placing them inside a UIView

Center align (horizontal and vertically) 2 UIViews inside a container (UIView), without placing them inside a UIView. I know it can be done by placing them inside a UIView or UIStackView.

But how can we do it directly ??

---INTERVIEW QUESTION---

REF : -- Sample output

Everyone is making things complicated...

The easiest solution :

  • Add Center Horizontal constraint for all the view
  • Add Top constraint for View2 wrt. View1.
  • Add Center Vertical constraint to View1, with constant as -(Height of View1 + Height of View2 + spacing between them)/2.

Thanks 🙏 to the interviewer only 😉.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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