在iOS中创建“流程布局”类型布局的最佳/最简单方法是什么? [英] What is the best/easiest way to create 'flow layout' type layout in iOS

查看:81
本文介绍了在iOS中创建“流程布局”类型布局的最佳/最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Q1。我有三个控件: UILabel UIButton UILabel 在一条线上。我想以编程方式一个接一个地排列它们,没有任何间隙(类似于Java / AndroidFlowlayout布局),因为每个控件上的文本长度将因用户操作而改变。以编程方式实现Flowlayout布局的最佳/最简单方法是什么?



Q2。与上述相关,我希望每个控件在文本因用户操作而发生更改时自动调整大小,因此全文仍然可见。



提前致谢。



//编辑2011年11月12日



以下是我计划如何实现控件的水平流程布局包含在viewArray中:

   - (void)doHorizo​​ntalFlowLayout:(NSArray *)viewArray 
{
if( viewArray == nil || viewArray.count< = 1
return; //离开这里,无需继续

UIView * v0 =(UIView *)[viewArray objectAtIndex: 0]; //第一个视图
CGRect frame0 = v0.frame;

CGFloat sumWidth = 0;
for(int i = 1; i< viewArray.count; i ++ )
{
UIView * thisView =(UIView *)[viewArray objectAtIndex:i];
sumWidth = sumWidth + v0.frame.size.width;
CGRect nextFrame = CGRectMake(frame0 .origin.x
+ sumW idth,thisView.frame.origin.y,
thisView.frame.size.width,thisView.frame.size.height);
thisView.frame = nextFrame;
//以上仅适用于2个视图。对于2个以上的视图 - 将v0重置为指向第i个视图
v0 =(UIView *)[viewArray objectAtIndex:i];

}
}


解决方案

正在努力解决这个问题 - 这是我的解决方案:



iOS中没有流程布局概念,因此您必须计算每个控件的宽度手动并将它们绝对放在父容器中。 UILabels与按钮略有不同,因为确定内容大小的唯一方法是使用 [NSString sizeWithFont:...] 方法。有关示例,请参见此处。 / p>

在您的情况下,您必须在任何元素更改时监听用户交互完成的时间,并使用 [UIView setNeedsDisplay] (http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/setNeedsDisplay)方法重新绘制视图和子视图。



这比它需要的要困难得多,恕我直言。


Q1. I have three controls: UILabel, UIButton, and UILabel in a single line. I want to programmatically arranged them one after another in a line, without any gaps (similar to Java/Android "Flowlayout" layout), because length of text on each control will change due to the user actions. What is the best/easiest way to achieve "Flowlayout" layout programmatically?

Q2. Related to above, I want each control to resize automatically when the text change due to user actions, so the full text remains visible.

Thanks in advance.

//Edited 11/12/2011

Here is how I plan to achieve horizontal "flow layout" of controls that are contained in viewArray:

-(void) doHorizontalFlowLayout:(NSArray *) viewArray
{
        if(viewArray == nil || viewArray.count <=1
            return; //get out of here, no need to continue

        UIView *v0= (UIView *) [viewArray objectAtIndex:0]; // first view
        CGRect frame0 = v0.frame;

        CGFloat sumWidth= 0;
        for(int i=1; i < viewArray.count; i++)
        {
            UIView *thisView= (UIView*) [viewArray objectAtIndex:i];
            sumWidth = sumWidth+ v0.frame.size.width;
            CGRect nextFrame= CGRectMake(frame0.origin.x
                    +sumWidth, thisView.frame.origin.y,
                     thisView.frame.size.width, thisView.frame.size.height);
            thisView.frame= nextFrame;
            //the above works for 2 views only. For more than 2 views - reset v0 to point to the ith view
            v0 = (UIView*) [viewArray objectAtIndex:i];

        }
 }

解决方案

Was just struggling with this -- here is my solution:

There's no "flow layout" concept in iOS, so you have to calculate the widths of each control manually and place them absolutely in the parent container. UILabels are a bit different than buttons, as the only way to determine the content size is with the [NSString sizeWithFont:...] method. See here for an example.

In your case, you'll have to listen for when user interaction is complete on whichever element is changing, and use the [UIView setNeedsDisplay] (http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/setNeedsDisplay) method to redrawn the view and subviews.

This is much more difficult than it needs to be, IMHO.

这篇关于在iOS中创建“流程布局”类型布局的最佳/最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆