[英]Swift iOS : Programmatically create a responsive menu using UIButton and a View and constraints
In the viewDidLoad()
method I would like to create 10 UIButtons, title them 在
viewDidLoad()
方法中,我想创建10个UIButton,为其命名
["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
and add them to a View in a way that they are always responsively placed using constraints. 并以始终使用约束响应性地放置它们的方式将它们添加到视图中。
I would like to the UIButton
to a View
which is always on the top of the page but is responsive to User positioning of the device and the text that is placed into the UIButton.title
. 我想将
UIButton
设置为始终位于页面顶部的View
,但它响应用户对设备的定位以及放置在UIButton.title
的文本。
This is the same thing that Bootstrap CSS does using the CSS classes "table", "row", "col-md-2". 这与Bootstrap CSS使用CSS类“ table”,“ row”,“ col-md-2”一样。
If the titles of the buttons change, the code should auto-magically adjust the positioning as such: 如果按钮的标题更改,则代码应自动修改位置,如下所示:
Use UICollectionView
and UICollectionViewFlowLayout
, make your controller conform to UICollectionViewDelegateFlowLayout
protocol and then for each cell just calculate the width of the UIButton
and then provide the size to the UICollectionViewDelegateFlowLayout
使用
UICollectionView
和UICollectionViewFlowLayout
,使您的控制器符合UICollectionViewDelegateFlowLayout
协议,然后对于每个单元格只需计算UIButton
的宽度,然后将大小提供给UICollectionViewDelegateFlowLayout
Something like: 就像是:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let itemWidth = ...//calculate the width of the button
return CGSize(width: kCGImagePropertyIPTCExtMaxAvailWidth, height: 50)//set height to whatever you want
}
就像评论者说的.....使用堆栈视图一样,集合视图是过大的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.