简体   繁体   中英

Xamarin iOS 7.1 - UIScrollview not scrolling

I'm working on an app in Xamarin where I am using a UIScrollView control that scrolls just fine in iOS 8.4; however, when I test it in iOS 7.1, the content within the UIScrollView does not scroll.

public class ScrollingCodeSampleViewController : ViewController
    {
        private readonly UIFont BODY_FONT = UIFont.FromName("HelveticaNeue", 14f);
        private UIScrollView _scrollView;
        private UILabel _label;

        public ScrollingCodeSampleViewController () : base()
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            BuildView ();
        }

        public override void ViewDidAppear (bool animated)
        {
            base.ViewWillAppear (animated);
            _scrollView.ContentSize = new CoreGraphics.CGSize (View.Bounds.Width, (_label.Frame.Y + _label.Frame.Height));
        }

        private void BuildView(){
            _scrollView = new UIScrollView (){ TranslatesAutoresizingMaskIntoConstraints = false, ScrollEnabled = true };
            View.Add (_scrollView);

            _label = new UILabel(){TranslatesAutoresizingMaskIntoConstraints = false, Font = BODY_FONT, TextColor = UIColor.White, Lines = 0, Text = "A lot of text here"};
            _label.SizeToFit ();
            _scrollView.Add (_label);

            View.AddConstraints (new [] {
                NSLayoutConstraint.Create(_scrollView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, View, NSLayoutAttribute.Top, 1, 55),
                NSLayoutConstraint.Create(_scrollView, NSLayoutAttribute.Left, NSLayoutRelation.Equal, View, NSLayoutAttribute.Left, 1, 0),
                NSLayoutConstraint.Create(_scrollView, NSLayoutAttribute.Right, NSLayoutRelation.Equal, View, NSLayoutAttribute.Right, 1, 0),
                NSLayoutConstraint.Create(_scrollView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, View, NSLayoutAttribute.Bottom, 1, -50),

                NSLayoutConstraint.Create(_label, NSLayoutAttribute.Top, NSLayoutRelation.Equal, _scrollView, NSLayoutAttribute.Top, 1, 0),
                NSLayoutConstraint.Create(_label, NSLayoutAttribute.Left, NSLayoutRelation.Equal, View, NSLayoutAttribute.Left, 1, 15),
                NSLayoutConstraint.Create(_label, NSLayoutAttribute.Right, NSLayoutRelation.Equal, View, NSLayoutAttribute.Right, 1, -15)
            });
        }
    }

When debugging in both 8.4 and 7.1, _label.Frame is {15, 0, 290, 793} which is enough to cause the scrollview to scroll. However, in 7.1, it is not scrolling. I feel like I'm missing something simple; however, I cannot locate it.

尽管ViewDidAppear(bool)在iOS 8.4和iOS 9上正常工作,但ScrollView并未设置contentSize,直到我在ViewController的ViewDidLayoutSubviews()事件期间对其进行设置为止。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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