locked
how to create horizontal scroll view RRS feed

  • Question

  • User306227 posted

    how to create horizontal scroll view

    Monday, May 11, 2020 7:23 AM

Answers

  • User369979 posted

    ScrollView on iOS has a content size property. When its value is larger than scroll view itself, the scroll view could be scrolled. So here I create a simple scroll view for demonstrating:

    UIScrollView scrollview = new UIScrollView(new CGRect(0, 100, 300, 100));
    
    UIView view = new UIView();
    view.TranslatesAutoresizingMaskIntoConstraints = false;
    view.BackgroundColor = UIColor.SystemPinkColor;
    scrollview.AddSubview(view);
    
    view.LeadingAnchor.ConstraintEqualTo(scrollview.LeadingAnchor).Active = true;
    view.TopAnchor.ConstraintEqualTo(scrollview.TopAnchor).Active = true;
    view.TrailingAnchor.ConstraintEqualTo(scrollview.TrailingAnchor).Active = true;
    view.BottomAnchor.ConstraintEqualTo(scrollview.BottomAnchor).Active = true;
    
    view.WidthAnchor.ConstraintEqualTo(1000).Active = true;
    view.HeightAnchor.ConstraintEqualTo(50).Active = true;
    
    this.View.AddSubview(scrollview);
    

    Using autolayout to populate the children could automatically calculate its content size. The child view here has its own size(1000, 50) and I use the constraints to set up its position compared to the parent scroll view. Finally, the content size could be determined. Here is the effect:

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, May 12, 2020 6:04 AM

All replies

  • User391726 posted

    Use this in your XAML Orientation="Horizontal"

    Monday, May 11, 2020 10:02 AM
  • User306227 posted

    i need in xamarin.ios native ..not in xamarin forms

    Monday, May 11, 2020 10:04 AM
  • User369979 posted

    ScrollView on iOS has a content size property. When its value is larger than scroll view itself, the scroll view could be scrolled. So here I create a simple scroll view for demonstrating:

    UIScrollView scrollview = new UIScrollView(new CGRect(0, 100, 300, 100));
    
    UIView view = new UIView();
    view.TranslatesAutoresizingMaskIntoConstraints = false;
    view.BackgroundColor = UIColor.SystemPinkColor;
    scrollview.AddSubview(view);
    
    view.LeadingAnchor.ConstraintEqualTo(scrollview.LeadingAnchor).Active = true;
    view.TopAnchor.ConstraintEqualTo(scrollview.TopAnchor).Active = true;
    view.TrailingAnchor.ConstraintEqualTo(scrollview.TrailingAnchor).Active = true;
    view.BottomAnchor.ConstraintEqualTo(scrollview.BottomAnchor).Active = true;
    
    view.WidthAnchor.ConstraintEqualTo(1000).Active = true;
    view.HeightAnchor.ConstraintEqualTo(50).Active = true;
    
    this.View.AddSubview(scrollview);
    

    Using autolayout to populate the children could automatically calculate its content size. The child view here has its own size(1000, 50) and I use the constraints to set up its position compared to the parent scroll view. Finally, the content size could be determined. Here is the effect:

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, May 12, 2020 6:04 AM