float:left in objective-c

谁说我不能喝 提交于 2019-12-21 04:05:00

问题


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 they fit within their container view.

In landscape mode, this UIScrollView should scroll horizontally, in portrait mode, it should scroll vertically.

I'm trying to make an ScrollView similar to the "Featured" tab in the iPad YouTube app. Landscape has 4 columns, portrait: 3 columns, "Subscriptions" tab, portrait, the same view has 2 columns.


回答1:


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.




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/4649330/floatleft-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!