简体   繁体   English

Xamarin iOS-使scrollview的内容适合scrollview

[英]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滚动视图的内容大小:

Solution

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.

相关问题 如何淡出 Xamarin Forms iOS 中的滚动视图的内容? - How do I fade out the contents of a scrollview in Xamarin Forms iOS? 我的滚动视图Xamarin iOS的结构 - Structure of my scrollview xamarin ios 如何使用AutoLayout(iOS / Xamarin.iOS)使ScrollView适合其内容? - How to make ScrollView fit its content using AutoLayout (iOS/Xamarin.iOS)? iOS:如何将内容从滚动视图复制到另一个滚动视图 - iOS: How to copy contents from a scrollview into another scrollview Xamarin Forms-iOS输入字段不会自动调整位置,因为键盘出现在ScrollView中 - Xamarin Forms - iOS Input fields does not adjust position automatically as keyboard appears with contents inside ScrollView Swift - 滚动视图中的内容被砍掉 - Swift - contents within scrollview get chop off 使scrollview的内容适合scrollview超级视图 - Fit content of a scrollview to the scrollview superview 如何使水平 ScrollView 适合内容的高度? - How to fit horizontal ScrollView to content's height? Xamarin iOS在StoryBoard中将项目添加到ScrollView - Xamarin iOS Add Items to a ScrollView in the StoryBoard Xamarin.iOS ScrollView与UIPageViewController的使用,UIScrollView - Xamarin.iOS ScrollView with the use of UIPageViewController, UIScrollView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM