简体   繁体   English

将UIView添加到ScrollView进行分页

[英]Add a UIView to a ScrollView for paging

How can I have a UIScollView content be the content from a UIView ? 如何让UIScollView内容成为UIView的内容? I want to design the app layout in a UIView and then lay that view into a UIScrollView that is connected to a UIPageControl for pagination. 我想在UIView设计应用程序布局,然后将该视图放入连接到UIPageControl进行分页的UIScrollView中。 So when the user swipes to the side, the next view is displayed. 因此,当用户滑动到侧面时,将显示下一个视图。 I have a sort of idea on how I would accomplish this, but I want to get it right without wasting a lot of time. 对于如何实现此目标,我有一种想法,但我希望在不浪费大量时间的情况下正确解决问题。

Heres my DetailViewController: 这是我的DetailViewController:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

using UIKit;
using Foundation;
using CoreGraphics;
using CloudKit;

namespace RecordStorePro
{
    public partial class DetailViewController : UIViewController
    {
        public Record DetailRecord { get; set; }

        public DetailViewController (IntPtr handle) : base (handle)
        {
        }

        public void SetDetailRecord (Record record)
        {
            if (DetailRecord != record) {
                DetailRecord = record;

                // Update the view
                ConfigureView ();
            }
        }

        void ConfigureView ()
        {
            // Update the user interface for the detail item
            if (IsViewLoaded && DetailRecord != null) {
                //label.Text = DetailRecord.Album;
            }
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
            // Perform any additional setup after loading the view, typically from a nib.

            NavigationItem.SetLeftBarButtonItem (new UIBarButtonItem(UIBarButtonSystemItem.Stop, (sender, args) => {
                NavigationController.PopViewController(true);
            }), true);

            ConfigureView ();

            //this.scrollview.BackgroundColor = UIColor.Gray;

            // set pages and content size
            scrollview.ContentSize = new SizeF ((float)(scrollview.Frame.Width * 2), (float)(scrollview.Frame.Height));
            //this.scrollview.AddSubview ();

            this.scrollview.Scrolled += ScrollEvent;
        }

        private void ScrollEvent(object sender, EventArgs e)
        {
            this.pagecontrol.CurrentPage = (int)System.Math.Floor(scrollview.ContentOffset.X / this.scrollview.Frame.Size.Width);
        }

        public override void DidReceiveMemoryWarning ()
        {
            base.DidReceiveMemoryWarning ();
            // Release any cached data, images, etc that aren't in use.
        }
    }
}

So when I swipe the screen, I want a subview that contains some labels and textfields to come in and replace the original labels and textfields. 因此,当我滑动屏幕时,我想要一个包含一些标签和文本字段的子视图,以替换原始标签和文本字段。 It works properly so far except I can't figure out how to add the subview, and make it size appropriately to different screen sizes. 到目前为止,它可以正常工作,除非我不知道如何添加子视图,并使其大小适合不同的屏幕尺寸。

EDIT 编辑

Heres the problem Im facing now, the views laying in the ScrollView act funny and are about 44f too tall so they let me drag up and down. 这就是我现在面临的问题,位于ScrollView中的视图表现得很有趣,并且高度大约为44f,因此它们让我上下拖动。 i tried setting all kinds of constraints as well as manually setting them to -44 smaller with no help. 我尝试设置各种约束以及在没有帮助的情况下将它们手动设置为-44小。 heres a picture of the problem now: 现在是问题的照片:

新问题

Heres the screenshot of my constraints set. 这是我的约束集的屏幕截图。

View A: 查看A: 查看A

View B: 查看B: 查看B

ScrollView: ScrollView: 滚动视图

Nib view: 笔尖视图: 在此处输入图片说明

To do this, you might want to try these steps: 为此,您可能需要尝试以下步骤:

  1. The view that shows the first page will be called View A . 显示第一页的视图称为视图A。 The view that shows the second page will be called View B . 显示第二页的视图称为View B。
  2. Add both views to the Scroll View. 将两个视图添加到滚动视图。
  3. Control -drag from View A to the Scroll View in the sidebar. 按住Control键-从视图A拖动到侧边栏中的滚动视图。
  4. Hold down Shift and select Top Space to Superview , Bottom Space to Superview , and Leading Space to Superview . 按住Shift键,然后选择“ Top Space to Superview ,“ Bottom Space to Superview ”以及“ Leading Space to Superview Next, press Return to add those constraints. 接下来,按回车键添加这些约束。
  5. Make sure those constraints' constants are set to 0 . 确保这些约束的常量设置为0
  6. Control -drag from View B to the Scroll View in the sidebar. 按住Control键并将其从视图B拖动到侧栏中的滚动视图。
  7. Hold down Shift and select Top Space to Superview , Bottom Space to Superview , and Trailing Space to Superview . 按住Shift键,然后选择“ Top Space to Superview ,“ Bottom Space to Superview ”以及“ Trailing Space to Superview Next, press Return to add those constraints. 接下来,按回车键添加这些约束。
  8. Control -drag from View A to View B in the sidebar. 在侧边栏中, 按住Control键从View A拖动到ViewB。
  9. Select Horizontal Spacing . 选择Horizontal Spacing Make sure its constant is 0 and its Second Item is View A.Trailing and its First Item is View B.Leading . 确保其常数为0 ,其Second ItemView A.Trailing ,其First ItemView B.Leading
  10. Control -drag from View A to View B in the sidebar. 在侧边栏中, 按住Control键从View A拖动到ViewB。 Select Equal Widths . 选择Equal Widths Make sure the Constant is 0 . 确保常量为0
  11. Control -drag from View A to the Scroll View in the sidebar. 按住Control键-从视图A拖动到侧边栏中的滚动视图。 Select Equal Widths . 选择Equal Widths Make sure the constant is set to 0 . 确保常数设置为0
  12. In the inspector, check “Paging Enabled.” 在检查器中,选中“已启用寻呼”。 在“属性”检查器中“滚动”标题下

Adding subScrollView with no pagingEnabled to each page and controls into subScrollView works! 在每个页面上添加没有分页启用的subScrollView并将控件添加到subScrollView中是可行的! If you add controls directly to scrollview with paging enabled, it seems gesture recogniser's first responder is always scrollview so your controls never gets the event and behave like a disabled! 如果您在启用分页的情况下将控件直接添加到滚动视图,则手势识别器的第一响应者似乎始终是滚动​​视图,因此您的控件永远不会获得事件,并且表现得像残疾人一样!

UIScrollView has a property called “pagingEnabled” UIScrollView具有一个称为“ pagingEnabled”的属性

In Interface Builder, resize the scroll view to allow for some space below it for the page control. 在Interface Builder中,调整滚动视图的大小,以在页面控件下方留出一些空间。 Next, drag in a UIPageControl from the library, centered below the scroll view. 接下来,从库中将UIPageControl拖动到滚动视图的下方。 Resize the UIPageControl to take up the full width of the view. 调整UIPageControl的大小以占据视图的整个宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM