在两种布局的UIButton约束上 [英] Uibutton contraints in two layouts
问题描述
我仍然在试图让我周围的约束头和c一般最佳实践的目标。
I’m still trying to get my head around constraints and the objective c best practises in general.
我需要依赖于一个变量1至6 UIButtons之间动态创建。我确定这个部分,但与如何定位他们给混淆起来:
I need to dynamically create between 1 to 6 UIButtons depending on a variable. I’m ok with this part but confused with how to position them given that:
- 按钮的数量可以从1-6是任何东西。
- 按钮具有相等的大小。
- 横向和纵向都会有不同的布局。
- 他们必须留在横向视图浮动和垂直对齐在纵向视图顶部。
我猜我需要一个容器,它位于在视图的底部,取决于方向改变高度,那么有根据容器内的宽度按钮的规则。但如何去了解它,我不知道。
I'm guessing I'll need one container that sits at the bottom of the view and changes height depending on the orientation, then have rules for the buttons inside depending on the container width. But how to go about it I have no idea.
推荐答案
在长远来看,这将更好地使用 UICollectionView
。子类 UICollectionView
并在故事板添加。使集合视图的出口到您的视图控制器,并设置其委托和数据源。添加< UICollectionViewDelegate>
和< UICollectionViewDataSource>
到您的视图控制器和执行委托方法。对于按钮,使自定义 UICollectionViewCell
与的UIButton
作为其子视图。然后在细胞:ForRowAtIndexpath:
方法,初始化 UICollectionViewCell
的子类,并设置的UIButton
。这可能不是你想要的答案,但 UICollectionView
至于布局关心offeres更大的灵活性。你可以阅读更多关于 UICollectionViewDelegateFlowLayout
此处。
In the long run it will be better to use the UICollectionView
. Subclass UICollectionView
and add it in your Storyboard. Make an outlet of the collection view to your view controller and set its delegate and data source. Add the <UICollectionViewDelegate>
and <UICollectionViewDataSource>
to your view controller and implement the delegate methods. For the buttons, make a custom UICollectionViewCell
with a UIButton
as its subview. Then in cell:ForRowAtIndexpath:
method, initialize the UICollectionViewCell
subclass, and set the properties of the UIButton
. This might not be the answer you wanted, but UICollectionView
offeres much more flexibility as far as layouts are concerned. You can read more about UICollectionViewDelegateFlowLayout
here.
这篇关于在两种布局的UIButton约束上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!