ios – 如何使用AutoLayout将UIButtons放置在水平线(包装,左对齐)?

ios – 如何使用AutoLayout将UIButtons放置在水平线(包装,左对齐)?,第1张

概述我需要在我的应用程序(iOS 6.0及更高版本)中以编程方式创建几个UIButtons. 我想以“环绕”样式显示按钮:从左边缘开始,每个按钮应该水平放置(按照定义的顺序),如果按钮不符合当前的“行” ,它应该在上一行下方的左边缘开始一条新行. 注意:我不想要一个表格/网格,因为这些按钮有不同的宽度,我想要有一个右边彼此. 我可以手动计算代码中每个按钮的框架,但是我应该使用AutoLayout(以编 我需要在我的应用程序(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];}
总结

以上是内存溢出为你收集整理的ios – 如何使用AutoLayout将UIButtons放置在水平线(包装,左对齐)?全部内容,希望文章能够帮你解决ios – 如何使用AutoLayout将UIButtons放置在水平线(包装,左对齐)?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1065511.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存