[英]Xamarin iOS - Fit scrollview's contents within the scrollview
This may be a fairly simple solution but none of the suggestions here work for me. 这可能是一个非常简单的解决方案,但是这里没有建议对我有用。 I have a UIScrollView inside a UITableViewCell.
我在UITableViewCell中有一个UIScrollView。 I'm adding dynamic images from a list into the scrollview like this.
我正在将列表中的动态图像添加到滚动视图中,如下所示。
public void InitViews()
{
scrollViewThumbNails.ContentSize =
new SizeF((float)scrollViewThumbNails.Frame.Size.Width/listCount * listCount,
(float)scrollViewThumbNails.Frame.Size.Height);
for (int i = 0; i < listCount; i++)
{
var imageView = new UIImageView
{
Frame = new RectangleF((float)i * (float)scrollViewThumbNails.Frame.Size.Width / listCount, 0,
(float)scrollViewThumbNails.Frame.Size.Width / listCount, (float)scrollViewThumbNails.Frame.Size.Height),
UserInteractionEnabled = true,
ContentMode = UIViewContentMode.ScaleAspectFit
};
//call method to load the images
var index = i;
imageView.SetImage(
url: new NSUrl(allItems[i].AbsoluteUri),
placeholder: UIImage.FromFile("placeholder.png"),
completedBlock: (image, error, type, url) =>
{
//when download completes add it to the list
if (image != null)
{
allImages.Add(image);
scrollViewThumbNails.AddSubview(imageView);
}
});
}
I found a suggestion here which advices to set the content size of the scrollview, based on the total number of subviews in ViewDidAppear
like this: 我在这里找到了一个建议,该建议基于ViewDidAppear中的子视图总数,设置
ViewDidAppear
视图的内容大小,如下所示:
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
CGRect contentRect = CGRect.Empty;
foreach(UIImageView view in scrollViewThumbNails.Subviews)
{
contentRect = CGRect.Union(contentRect,view.Frame);
}
scrollViewThumbNails.ContentSize = contentRect.Size;
}
The imageViews are still spaced out and do not start from the edge of the screen/scrollview as shown in my screen shot below. 如下面的屏幕快照所示,imageViews仍然间隔开,并且不是从屏幕/滚动视图的边缘开始。 I would like for the first image to always be positioned at the origin of the scrollview and wouldn't want the spacing between each image.
我希望第一个图像始终位于滚动视图的原点,并且不希望每个图像之间都有间距。 How can I adjust the scrollview based on the size of its contents?
如何根据内容的大小调整滚动视图?
Can someone show me what I'm missing? 有人可以告诉我我所缺少的吗? Thanks.
谢谢。
I found a solution here where I calculate the total height and width of all the views and assign that as the content size of the scroll view in ViewDidLayoutSubviews
: 我在这里找到了一种解决方案,可以计算所有视图的总高度和宽度,并将其分配为
ViewDidLayoutSubviews
滚动视图的内容大小:
public override void ViewDidLayoutSubviews ()
{
base.ViewDidLayoutSubviews ();
try{
float scrollViewHeight = 0.0f;
float scrollViewWidth = 0.0f;
foreach(UIImageView view in scrollViewThumbnails.Subviews)
{
scrollViewWidth += (float)view.Frame.Size.Width;
scrollViewHeight += (float)view.Frame.Size.Height;
}
scrollViewThumbnails.ContentSize = new CGSize(scrollViewWidth, scrollViewHeight);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message+ex.StackTrace);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.