ios - swift iOS : Programmatically create a responsive menu using UIButton and a View and constraints

标签 ios swift responsive-design

viewDidLoad() 方法中,我想创建 10 个 UIButton,给它们添加标题
[“一”、“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”、“十”]
并将它们添加到 View 中,使其始终使用约束进行响应式放置。

我想要将UIButton添加到View,它始终位于页面顶部,但响应设备的用户定位和放置的文本进入UIButton.title

这与 Bootstrap CSS 使用 CSS 类“table”、“row”、“col-md-2”所做的事情相同。

布局示例: enter image description here

如果按钮的标题发生变化,代码应该自动调整位置,如下所示:

enter image description here

最佳答案

使用UICollectionViewUICollectionViewFlowLayout,使您的 Controller 符合UICollectionViewDelegateFlowLayout协议(protocol),然后为每个单元格计算的宽度UIButton ,然后将大小提供给 UICollectionViewDelegateFlowLayout

类似于:

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
}

关于ios - swift iOS : Programmatically create a responsive menu using UIButton and a View and constraints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50117579/

相关文章:

iphone - 在模拟器中启动 iOS 应用程序时出现白色 View

ios - 如何将自动对焦添加到 AVCaptureSession? swift

javascript - 如何动态地将高度添加到包含响应图像的 div

html - 无限期地在 bootstrap 中循环 divs?

ios - UIWebView 弹跳回调

ios - 来自多对多关系的核心数据详细信息 View

ios - 有没有办法在 SFSafariViewController 发出的初始请求中设置 referer?

ios - YouTube - 下载/播放私有(private)视频

swift - 为什么需要在类外定义可等式协议(protocol)?

html - 如何让按钮水平对齐(使用 Bootstrap )