float:left in objective-c

后端 未结 3 2059
执念已碎
执念已碎 2021-02-14 18:54

I\'m trying to make a bunch of buttons behave somewhat like float:left in CSS. So whenever the view changes size, on orientation change for example, the buttons should adjust so

相关标签:
3条回答
  • 2021-02-14 19:27

    I have implemented a layout system to do things like this. There is a demo project on how to use it checked into that repository. I'd be happy to answer any questions about it. This is much more lightweight than AQGridView, so if you don't need the extra functionality he is providing, I would recommend an approach similar to mine.

    0 讨论(0)
  • 2021-02-14 19:42

    If you had to implement this manually (by overriding layoutSubviews), I think the algorithm would be something like this:

    1. Start with X = 0, Y = 0 (assuming flipped coordinates)
    2. For each button:
      1. If (X + button width) > container width, set X = 0, increase Y
      2. Place button at (X, Y)
      3. Increase X by button's width
    0 讨论(0)
  • 2021-02-14 19:45

    you might want to check out AQGridView, which is basically a re-implementation of Cocoa's NSCollectionView: https://github.com/AlanQuatermain/AQGridView

    Otherwise, you might need to override layoutSubviews in your parentView and then rearrange the subviews (buttons) accordingly when the dimensions of the parentView change.

    Cheers,

    Johannes

    0 讨论(0)
提交回复
热议问题