[英]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小。 現在是問題的照片:
這是我的約束集的屏幕截圖。
為此,您可能需要嘗試以下步驟:
Top Space to Superview
,“ Bottom Space to Superview
”以及“ Leading Space to Superview
。 接下來,按回車鍵添加這些約束。 0
。 Top Space to Superview
,“ Bottom Space to Superview
”以及“ Trailing Space to Superview
。 接下來,按回車鍵添加這些約束。 Horizontal Spacing
。 確保其常數為0
,其Second Item
為View A.Trailing
,其First Item
為View B.Leading
。 Equal Widths
。 確保常量為0
。 Equal Widths
。 確保常數設置為0
。 在每個頁面上添加沒有分頁啟用的subScrollView並將控件添加到subScrollView中是可行的! 如果您在啟用分頁的情況下將控件直接添加到滾動視圖,則手勢識別器的第一響應者似乎始終是滾動視圖,因此您的控件永遠不會獲得事件,並且表現得像殘疾人一樣!
UIScrollView具有一個稱為“ pagingEnabled”的屬性
在Interface Builder中,調整滾動視圖的大小,以在頁面控件下方留出一些空間。 接下來,從庫中將UIPageControl拖動到滾動視圖的下方。 調整UIPageControl的大小以占據視圖的整個寬度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.