我正在创建一个需要能够在视图之间滑动的应用。我使用了来自here的示例代码来开始。我遇到的问题是,当我从一个ViewController
转到分页ViewControllers
然后再返回时,ViewControllers
会被添加到列表中并且列表会不断增长。我在屏幕上看到了可以滚动的重复ViewControllers
。新列表刚刚添加到已填充的viewControllers
。
我没有在主屏幕上使用导航控制器,它只是一个带有按钮的滚动视图。这是HomeViewController类的一部分:
private void BuildScrollView()
{
float padding = 0.0f;
float h = 80.0f;
UIView content = new UIView (new RectangleF (15, 130, UIScreen.MainScreen.Bounds.Width - 30, UIScreen.MainScreen.Bounds.Height));
content.BackgroundColor = UIColor.FromRGB(ColorManager.Grey_undrline_RGB[0], ColorManager.Grey_undrline_RGB[1], ColorManager.Grey_undrline_RGB[2]);
UIScrollView scrollView = new UIScrollView {
Frame = new RectangleF(2, 2, content.Frame.Width - 4 , content.Frame.Height -4),
BackgroundColor = UIColor.FromRGB(ColorManager.Grey_backgrd_RGB[0], ColorManager.Grey_backgrd_RGB[1], ColorManager.Grey_backgrd_RGB[2]),
AutoresizingMask = UIViewAutoresizing.FlexibleWidth
};
List<myObject> prayerList = myObjectBuilder.GeObjectFiles ();
for(int i = 0; i < myList.Count; i++){
UIView btnView = new UIView(new RectangleF (padding , padding * (i + 1) + (i * h), scrollView.Frame.Width - (padding * 2), h));
btnView.BackgroundColor = UIColor.FromRGB (ColorManager.Grey_backgrd_RGB [0], ColorManager.Grey_backgrd_RGB [1], ColorManager.Grey_backgrd_RGB [2]);
myButton btnImage = new myButton (new RectangleF (3, 3, 135, 80));
UIImage tempImage = new UIImage ("Images/bottomImages/" + myList[i].bottomImage ).Scale(new SizeF(135, 80));
btnImage.BackgroundColor = UIColor.FromPatternImage (tempImage);
btnImage.PrayerID = i;
btnImage.TouchUpInside += (object sender, EventArgs e) => {
LoadPrayerView(sender);
};
btnView.AddSubview (btnImage);
myButton button = new myButton(new RectangleF (141, 3, btnView.Frame.Width - 145, h));
button.PrayerID = i;
button.BackgroundColor = UIColor.White;
button.SetTitle (myList[i].ShortTitle.ToUpper(), UIControlState.Normal);
button.SetTitleColor (UIColor.Black, UIControlState.Normal);
button.HorizontalAlignment = UIControlContentHorizontalAlignment.Left;
button.TitleLabel.Font = UIFont.FromName ("HelveticaNeue", 16.0f);
button.TitleLabel.Lines = 0;
button.TitleLabel.LineBreakMode = UILineBreakMode.WordWrap;
button.TitleEdgeInsets = new UIEdgeInsets (0, 10, 0, 10);
button.TouchUpInside += (object sender, EventArgs e) => {
LoadView(sender);
};
btnView.AddSubview (button);
scrollView.AddSubview (btnView);
}
scrollView.ContentSize = new SizeF (scrollView.Frame.Width, (prayerList.Count + 2) * (h + padding) + padding);
content.AddSubview (scrollView);
this.View.AddSubview (content);
}
如果需要,我可以发布更多代码。在这一点上,我不确定从哪里开始寻找解决方案。