[英]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. 这是我的约束集的屏幕截图。
To do this, you might want to try these steps: 为此,您可能需要尝试以下步骤:
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. 接下来,按回车键添加这些约束。 0
. 确保这些约束的常量设置为0
。 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. 接下来,按回车键添加这些约束。 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 Item
为View A.Trailing
,其First Item
为View B.Leading
。 Equal Widths
. 选择Equal Widths
。 Make sure the Constant is 0
. 确保常量为0
。 Equal Widths
. 选择Equal Widths
。 Make sure the constant is set to 0
. 确保常数设置为0
。 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.