MonoTouch UIScrollView不以动态UILabel高度滚动 [英] MonoTouch UIScrollView not scrolling with dyanmic UILabel height
问题描述
我有一个UILabel(名为contentLbl),它将在UIScrollView中动态更改.它上方是另一个UILabel(名为contentTitleLbl),它是一个标题,不包含动态内容.要成功滚动,我知道我必须至少将UIScrollView的ContentSize设置为内容视图的ContentSize.我还看到应该设置框架.我都尝试过,但仍然无法滚动.
I have a UILabel (named contentLbl) that will change dynamically within a UIScrollView. Above it is another UILabel (named contentTitleLbl) that is a title and does not contain dynamic content. To successfully scroll I know that I must at least set the ContentSize of the UIScrollView to that of the content view's. I've also seen that the Frame should be set. I have tried both but it still does not scroll.
在我的示例代码中,contentView包含contentLbl和contentTitleLbl. scrollView是对我当前尝试使用的UIScrollView的引用.
In my example code contentView contains both the contentLbl and contentTitleLbl. scrollView is the reference to the UIScrollView that I'm currently trying to use.
这是我当前的代码:
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
//contentLbl is to have dynamic text, and currently stores a large lorem ipsum paragraph
//contentTitleLbl is the title and is not dynamic
this.contentLbl.SizeToFit();
this.scrollView.ContentSize = new SizeF(this.contentView.Frame.Width, this.contentLbl.Frame.Size.Height + this.contentTitleLbl.Frame.Size.Height);
this.scrollView.Frame = new RectangleF(this.scrollView.Frame.X, this.scrollView.Frame.Y, this.scrollView.ContentSize.Width, this.scrollView.ContentSize.Height);
this.scrollView.AddSubview(this.contentView);
}
任何帮助将不胜感激!
推荐答案
快速问题:您打算标题随文本一起滚动吗?在此解决方案中,我已经做到了,因为这是我的解释.如果您愿意,我会对其进行调整,以使标题不会滚动. 我通过在XCode中创建此层次结构来重新创建了示例
Quick question: Are you intending for the title to scroll with your text? In this solution, I've done that, as this is my interpretation. I'll adjust it, if you'd like, for the title not to scroll. I recreated your sample by making this hierarchy in XCode
- UIView(视图控制器的主视图)
- UIView(contentView)
- UILabel(contentTitleLbl)
- Frame =(20,15,160,21)
- UIView (main view of the view controller)
- UIView (contentView)
- UILabel (contentTitleLbl)
- Frame = (20,15,160,21)
- 行数= 100
- Frame =(20,43,160,190)
- Text ="Lorem ipsum ..."(长段)
- Frame =(20,20,140,140)
我将所有项目的背景色设置为不同的值,以便可以看到所有组件的尺寸.
I set the background colors of all the items to different values so that I could see the dimensions of all the components.
首先,我需要确保所有标签的自动调整大小功能都已关闭.您可以使用XCode或以编程方式执行此操作:
First, I needed to make sure all the auto-resizing of the labels was turned off. You can do this in XCode, or programmatically:
contentLbl.AutoresizingMask = UIViewAutoresizing.None; contentTitleLbl.AutoresizingMask = UIViewAutoresizing.None;
我更愿意通过XCode将
contentView
放在scrollView
内,但是看起来您像在主View
下一样将它们作为同级.因此,我们将contentView
替换为scrollView
I'd prefer to place the
contentView
inside thescrollView
via XCode, but it looks like you had them as siblings under the mainView
. So we reparent thecontentView
to place it inside thescrollView
scrollView.AddSubview( contentView );
然后使标题的宽度与
scrollView
相同,将其定位在原点,并保持XCode设计的高度Then make the title the same width as the
scrollView
, positioning it at the origin and leaving the height as designed in XCodevar f = contentTitleLbl.Frame; f.X = 0; f.Y = 0; f.Width = scrollView.Frame.Width; contentTitleLbl.Frame = f;
然后是第一个主要问题:调整
UILabel
的大小以动态地适应您的内容.SizeToFit
将标签的大小调整为一行文本(或至少对我来说是如此).您真正想要的是NSString.sizeWithFont
,它在MonoTouch中显示为UIView.StringSize .注意,因为其中一些返回单行文本的大小.在这里,我将宽度限制为滚动视图的宽度,但是使用float.MaxValue
使高度不受限制.Then the first major issue: resizing a
UILabel
to fit your contents dynamically.SizeToFit
resizes the label to a single line of text (or at least, did for me). What you really want isNSString.sizeWithFont
which appears in MonoTouch as UIView.StringSize. Be careful, as some of these return the size for a single line of text. Here I've constrained the width to the width of the scroll view, but left the height unbounded by usingfloat.MaxValue
.SizeF sz = this.View.StringSize( contentLbl.Text, contentLbl.Font, new SizeF( scrollView.Frame.Width, float.MaxValue), UILineBreakMode.WordWrap );
然后调整标签的大小并将其放置在标题下方:
Then resize and position the label just underneath the title:
f = contentLbl.Frame; f.X = 0; f.Y = contentTitleLbl.Frame.Bottom; // nudge the contentLbl right up against the title f.Width = sz.Width; // size matches the measured values from above f.Height = sz.Height; contentLbl.Frame = f;
然后调整
contentView
的大小以容纳标题和内容.该视图将放置在滚动视图的原点.大小由上面的代码动态确定.我在底部添加了一个小边距,以便可以确定整个内容已经渲染Then adjust the size of the
contentView
to hold the title and the content. This view will be placed at the origin in the scroll view. The size is determined dynamically by the code above. I've added a small margin at the bottom so I can tell that the whole content has been renderedf = contentView.Frame; f.X = 0; f.Y = 0; f.Width = scrollView.Frame.Width; f.Height = contentLbl.Frame.Bottom + 20; contentView.Frame = f;
然后将滚动视图的
ContentSize
设置为contentView的大小.如果大小比滚动视图的大小大,则 Lorem就是这种情况Ipsum 文本-您将获得滚动显示.Then set the
ContentSize
of the scroll view to the size of the contentView. If the size is larger than the scroll view's size -- which should be the case with the Lorem Ipsum text -- you'll get your scrolling.scrollView.ContentSize = contentView.Frame.Size;
干杯.
这是我能想到的最简单的滚动视图.在红色区域上上下拖动将显示滚动条.当您到达底部时,滚动视图的绿色背景应显示.
Here's the simplest scroll view I could come up with. Dragging up and down on the red area should show the scroll bar. When you get to the bottom, the green background of the scroll view should show.
var v = new UIView( new RectangleF(0,0,200,1000)); v.BackgroundColor = UIColor.Red; var sv = new UIScrollView( new RectangleF(20,20,200,300)); sv.BackgroundColor = UIColor.Green; sv.ContentSize = v.Frame.Size; View.AddSubview(sv); sv.AddSubview(v);
我在XCode中创建了一个滚动视图,只需将背景色和内容大小设置为大于滚动视图的大小即可.在蓝色滚动视图上拖动时,滚动条应出现.
I created a scroll view in XCode and simply set the background color and the content size to be larger than the scroll view's size. When dragging on the blue scroll view, the scroll bars should appear.
designerScrollView.BackgroundColor = UIColor.Blue; designerScrollView.ContentSize = new SizeF(1000,1000);
这篇关于MonoTouch UIScrollView不以动态UILabel高度滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- UILabel (contentTitleLbl)
- UIView (contentView)
- UILabel(contentTitleLbl)
- UIView(contentView)