Q1.我有三个控件:UILabel,UIButton并且UILabel在一行中.我想以编程方式一个接一个地排列它们,没有任何间隙(类似于Java/Android"Flowlayout"布局),因为每个控件上的文本长度将因用户操作而改变.以编程方式实现"Flowlayout"布局的最佳/最简单方法是什么?
Q2.与上述相关,我希望每个控件在文本因用户操作而发生更改时自动调整大小,因此全文仍然可见.
提前致谢.
//编辑于11/12/2011
以下是我计划如何实现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];
}
}
Run Code Online (Sandbox Code Playgroud)
正在努力解决这个问题 - 这是我的解决方案:
iOS中没有"流布局"概念,因此您必须手动计算每个控件的宽度并将它们绝对放在父容器中.UILabels与按钮略有不同,因为确定内容大小的唯一方法是使用该[NSString sizeWithFont:...]方法.请看这里的例子.
在您的情况下,您必须在任何元素发生变化时收听完用户交互的时间,并使用[UIView setNeedsDisplay](http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView /UIView.html#//apple_ref/occ/instm/UIView/setNeedsDisplay)重绘视图和子视图的方法.
恕我直言,这比它需要的要困难得多.
| 归档时间: |
|
| 查看次数: |
4536 次 |
| 最近记录: |