将UIView添加到ScrollView进行分页
Add a UIView to a ScrollView for paging
如何让 UIScollView
的内容成为 UIView
的内容?我想在 UIView
中设计应用程序布局,然后将该视图放入连接到 UIPageControl
的 UIScrollView
以进行分页。因此,当用户向一侧滑动时,会显示下一个视图。我对如何完成这个有一些想法,但我想在不浪费很多时间的情况下把它做好。
这是我的 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.
}
}
}
所以当我滑动屏幕时,我想要一个包含一些标签和文本字段的子视图进来并替换原来的标签和文本字段。到目前为止它工作正常,除了我不知道如何添加子视图,并使其大小适合不同的屏幕尺寸。
编辑
这是我现在面临的问题,放置在 ScrollView 中的视图很有趣,而且大约 44f 太高了,所以它们让我可以上下拖动。我尝试设置各种约束以及在没有帮助的情况下手动将它们设置为更小的 -44。现在是问题的图片:
这是我的约束集的屏幕截图。
观点一:
视图 B:
滚动视图:
笔尖视图:
Adding subScrollView with no pagingEnabled to each page and controls into subScrollView works! 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”
In Interface Builder, resize the scroll view to allow for some space below it for the page control. Next, drag in a UIPageControl from the library, centered below the scroll view. Resize the UIPageControl to take up the full width of the view.
为此,您可能需要尝试以下步骤:
- 显示第一页的视图将被称为视图 A。显示第二页的视图将称为 View B.
- 将两个视图添加到滚动视图。
- Control-从视图 A 拖动到边栏中的滚动视图。
- 按住 Shift 和 select
Top Space to Superview
、Bottom Space to Superview
和 Leading Space to Superview
。接下来,按 Return 添加这些约束。
- 确保这些约束的常量设置为
0
。
- Control-从视图 B 拖动到边栏中的滚动视图。
- 按住 Shift 和 select
Top Space to Superview
、Bottom Space to Superview
和 Trailing Space to Superview
。接下来,按 Return 添加这些约束。
- Control-在边栏中从视图 A 拖动到视图 B。
- Select
Horizontal Spacing
。确保它的常量是 0
并且它的 Second Item
是 View A.Trailing
并且它的 First Item
是 View B.Leading
.
- Control-在边栏中从视图 A 拖动到视图 B。 Select
Equal Widths
。确保常量为 0
.
- Control-从视图 A 拖动到边栏中的滚动视图。 Select
Equal Widths
。确保常量设置为 0
.
- 在检查器中,选中“启用分页”。
如何让 UIScollView
的内容成为 UIView
的内容?我想在 UIView
中设计应用程序布局,然后将该视图放入连接到 UIPageControl
的 UIScrollView
以进行分页。因此,当用户向一侧滑动时,会显示下一个视图。我对如何完成这个有一些想法,但我想在不浪费很多时间的情况下把它做好。
这是我的 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.
}
}
}
所以当我滑动屏幕时,我想要一个包含一些标签和文本字段的子视图进来并替换原来的标签和文本字段。到目前为止它工作正常,除了我不知道如何添加子视图,并使其大小适合不同的屏幕尺寸。
编辑
这是我现在面临的问题,放置在 ScrollView 中的视图很有趣,而且大约 44f 太高了,所以它们让我可以上下拖动。我尝试设置各种约束以及在没有帮助的情况下手动将它们设置为更小的 -44。现在是问题的图片:
这是我的约束集的屏幕截图。
观点一:
视图 B:
滚动视图:
笔尖视图:
Adding subScrollView with no pagingEnabled to each page and controls into subScrollView works! 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”
In Interface Builder, resize the scroll view to allow for some space below it for the page control. Next, drag in a UIPageControl from the library, centered below the scroll view. Resize the UIPageControl to take up the full width of the view.
为此,您可能需要尝试以下步骤:
- 显示第一页的视图将被称为视图 A。显示第二页的视图将称为 View B.
- 将两个视图添加到滚动视图。
- Control-从视图 A 拖动到边栏中的滚动视图。
- 按住 Shift 和 select
Top Space to Superview
、Bottom Space to Superview
和Leading Space to Superview
。接下来,按 Return 添加这些约束。 - 确保这些约束的常量设置为
0
。 - Control-从视图 B 拖动到边栏中的滚动视图。
- 按住 Shift 和 select
Top Space to Superview
、Bottom Space to Superview
和Trailing Space to Superview
。接下来,按 Return 添加这些约束。 - Control-在边栏中从视图 A 拖动到视图 B。
- Select
Horizontal Spacing
。确保它的常量是0
并且它的Second Item
是View A.Trailing
并且它的First Item
是View B.Leading
. - Control-在边栏中从视图 A 拖动到视图 B。 Select
Equal Widths
。确保常量为0
. - Control-从视图 A 拖动到边栏中的滚动视图。 Select
Equal Widths
。确保常量设置为0
. - 在检查器中,选中“启用分页”。