ios – 如何使用AutoLayout将UIButtons放置在水平线(包装,左对齐
发布时间:2020-12-15 01:41:12 所属栏目:百科 来源:网络整理
导读:我需要在我的应用程序(iOS 6.0及更高版本)中以编程方式创建几个UIButtons. 我想以“环绕”样式显示按钮:从左边缘开始,每个按钮应该水平放置(按照定义的顺序),如果按钮不符合当前的“行”,它应该在上一行下方的左边缘开始一条新行. 注意:我不想要一个表格/
我需要在我的应用程序(iOS 6.0及更高版本)中以编程方式创建几个UIButtons.
我想以“环绕”样式显示按钮:从左边缘开始,每个按钮应该水平放置(按照定义的顺序),如果按钮不符合当前的“行”,它应该在上一行下方的左边缘开始一条新行. 注意:我不想要一个表格/网格,因为这些按钮有不同的宽度,我想要有一个右边彼此. 我可以手动计算代码中每个按钮的框架,但是我应该使用AutoLayout(以编程方式创建NSLayoutConstraints)吗?我需要如何设置? 编辑:阅读第07章第4章“中级自动布局”后,我不确定是否使用纯AutoLayout可以实现我所需要的“环绕”功能. 解决方法
我目前的解决方案如下所示:No AutoLayout,但是手动设置每种情况的正确约束(第一个按钮,新行中的最左边的按钮,任何其他按钮).
(我的猜测是,直接设置每个按钮的框架会导致比使用NSLayoutConstraints更可读的代码) NSArray *texts = @[ @"A",@"Short",@"Button",@"Longer Button",@"Very Long Button",@"More Button",@"Any Key"]; int indexOfLeftmostButtonOnCurrentLine = 0; NSMutableArray *buttons = [[NSMutableArray alloc] init]; float runningWidth = 0.0f; float maxWidth = 300.0f; float horizontalSpaceBetweenButtons = 10.0f; float verticalSpaceBetweenButtons = 10.0f; for (int i=0; i<texts.count; i++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button setTitle:[texts objectAtIndex:i] forState:UIControlStateNormal]; [button sizeToFit]; button.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:button]; // check if first button or button would exceed maxWidth if ((i == 0) || (runningWidth + button.frame.size.width > maxWidth)) { // wrap around into next line runningWidth = button.frame.size.width; if (i== 0) { // first button (top left) // horizontal position: same as previous leftmost button (on line above) NSLayoutConstraint *horizontalConstraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0f constant:horizontalSpaceBetweenButtons]; [self.view addConstraint:horizontalConstraint]; // vertical position: NSLayoutConstraint *verticalConstraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:verticalSpaceBetweenButtons]; [self.view addConstraint:verticalConstraint]; } else { // put it in new line UIButton *previousLeftmostButton = [buttons objectAtIndex:indexOfLeftmostButtonOnCurrentLine]; // horizontal position: same as previous leftmost button (on line above) NSLayoutConstraint *horizontalConstraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:previousLeftmostButton attribute:NSLayoutAttributeLeft multiplier:1.0f constant:0.0f]; [self.view addConstraint:horizontalConstraint]; // vertical position: NSLayoutConstraint *verticalConstraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:previousLeftmostButton attribute:NSLayoutAttributeBottom multiplier:1.0f constant:verticalSpaceBetweenButtons]; [self.view addConstraint:verticalConstraint]; indexOfLeftmostButtonOnCurrentLine = i; } } else { // put it right from previous buttom runningWidth += button.frame.size.width + horizontalSpaceBetweenButtons; UIButton *previousButton = [buttons objectAtIndex:(i-1)]; // horizontal position: right from previous button NSLayoutConstraint *horizontalConstraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:previousButton attribute:NSLayoutAttributeRight multiplier:1.0f constant:horizontalSpaceBetweenButtons]; [self.view addConstraint:horizontalConstraint]; // vertical position same as previous button NSLayoutConstraint *verticalConstraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:previousButton attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f]; [self.view addConstraint:verticalConstraint]; } [buttons addObject:button]; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |