Q1.我有三个控件:UILabel
, UIButton
, and UILabel
在一行中。我想以编程方式将它们一个接一个地排成一行,没有任何间隙(类似于Java/Android“Flowlayout”布局),因为每个控件上的文本长度会因用户操作而改变。以编程方式实现“Flowlayout”布局的最佳/最简单方法是什么?
Q2。与上述相关,我希望每个控件在文本因用户操作而更改时自动调整大小,以便全文保持可见。
提前致谢。
//编辑于 2011 年 11 月 12 日
以下是我计划如何实现 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];
}
}
我只是在为此苦苦挣扎——这是我的解决方案:
iOS 中没有“流布局”概念,因此您必须手动计算每个控件的宽度并将它们绝对放置在父容器中。 UILabels 与按钮有点不同,因为确定内容大小的唯一方法是使用[NSString sizeWithFont:...]
方法。看here http://blog.engage-encore.com/index.php/2010/12/17/uilabel-size-to-fit-tex/举个例子。
在您的情况下,您必须监听用户交互何时完成(无论哪个元素正在更改),并使用[UIView setNeedsDisplay]
(http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/setNeedsDisplay)方法重绘视图和子视图。
恕我直言,这比需要的要困难得多。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)