3
votes

What I'm trying to achieve. Whenever I have a UIViewController that inherits from my class SuperViewController, in viewDidLoad of my superview controller i take all the subviews of my UIViewController, create a UIScrollView, add all of the subviews to it and set this UIScrollView as the only child of UIViewController.

UIViewController structure before calling [super viewDidLoad];

UIViewController
  UIView
     subview1
     subview2
     subview3

after

UIViewController
  UIView
    UIScrollView
       subview1
       subview2
       subview3

with the constraints that concern UIView - subviews, moved to UIScrollView-subviews

The reason I'm doing this is because in our app we have a lot of dealing with scrollViews and I just want to write the code to add them once in the super class and have it enable/ disable scrolling based on the content size.

What i have currently in my superviewController(simplified)

 - (void)viewDidLoad
{
    [super viewDidLoad];

     if([self class]!=[SuperViewController class] && [self isKindOfClass:[SuperViewController class]])
     {
        //subclass called this method
         UIScrollView *scrollViewToAdd = [[UIScrollView alloc] init];
         UIView *originalView = self.view;

         NSArray* savedConstraints = self.view.constraints;
         for (UIView* view in self.view.subviews)
         {
            [scrollViewToAdd addSubview:view];
         }

         for (NSLayoutConstraint*oldConstr in savedConstraints)
         {
               //loop through all of the constraints associated with the UIView. If one of the items is UIView, replace it with UIScrollView
               //add all of these constraints to UIScrollView
               NSLayoutConstraint *newConstraint = [NSLayoutConstraint    constraintWithItem:oldConstr.firstItem==self.view?scrollViewToAdd:oldConstr.firstItem  attribute:oldConstr.firstAttribute relatedBy:oldConstr.relation  toItem:oldConstr.secondItem==self.view?scrollViewToAdd:oldConstr.secondItem  attribute:oldConstr.secondAttribute multiplier:oldConstr.multiplier constant:oldConstr.constant];
              newConstraint.priority = oldConstr.priority;
              [scrollViewToAdd addConstraint:newConstraint];

        }

     }
     //moving the subviews sets self.view to nil, thankfully i kept a reference so i can set it to the original value
     _scrollView = scrollViewToAdd;
     self.view = originalView;
     //add scrollview as a child
     [self.view addSubview:_scrollView];

     //add scrollView constraints
     id topGuide =self.topLayoutGuide;
     id bottomGuide = self.bottomLayoutGuide;

    _scrollViewToTop =  [[NSLayoutConstraint constraintsWithVisualFormat: @"V: [topGuide]-0-[_scrollView]"
                                                              options: 0
                                                              metrics: nil
                                                                views: NSDictionaryOfVariableBindings (topGuide, _scrollView )] firstObject];


    _scrollViewToBottom =  [[NSLayoutConstraint constraintsWithVisualFormat: @"V:[_scrollView]-0-[bottomGuide]"
                                                                options: 0
                                                                metrics: nil
                                                                  views:      NSDictionaryOfVariableBindings (bottomGuide, _scrollView)] firstObject];


     [self.view addConstraint:_scrollViewToTop];
     [self.view addConstraint:_scrollViewToBottom];




 }

This gives me constraint conflicts I don't understand

 (
"<_UILayoutSupportConstraint:0xb866db0 V:[_UILayoutGuide:0xf9b2750(0)]>",
"<_UILayoutSupportConstraint:0xb866cf0 V:|-(0)-[_UILayoutGuide:0xf9b2750]   (Names: '|':UIView:0x9e90480 )>",
"<_UILayoutSupportConstraint:0xb866f90 V:[_UILayoutGuide:0xb862070(0)]>",
"<_UILayoutSupportConstraint:0xb866ec0 _UILayoutGuide:0xb862070.bottom == UIView:0x9e90480.bottom>",
"<NSLayoutConstraint:0xb87a710 V:[_UILayoutGuide:0xf9b2750]-(0)-[UIScrollView:0x9e84b00]>",
"<NSLayoutConstraint:0xb861e80 V:[UIScrollView:0x9e84b00]-(0)-[_UILayoutGuide:0xb862070]>",
"<NSAutoresizingMaskLayoutConstraint:0xf9a5280 h=--& v=--& UIScrollView:0x9e84b00.midY ==>",
"<NSAutoresizingMaskLayoutConstraint:0xf992950 h=-&- v=-&- UIView:0x9e90480.height == UIViewControllerWrapperView:0xb87bd20.height - 64>",
"<NSAutoresizingMaskLayoutConstraint:0xb870f90 h=-&- v=-&- UIViewControllerWrapperView:0xb87bd20.height == UINavigationTransitionView:0x9a6e3e0.height>",
"<NSAutoresizingMaskLayoutConstraint:0xf9b3560 h=-&- v=-&- UINavigationTransitionView:0x9a6e3e0.height == UILayoutContainerView:0x9a6dd10.height>",
"<NSLayoutConstraint:0xb876390 'UIView-Encapsulated-Layout-Height' V:[UILayoutContainerView:0x9a6dd10(480)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0xb861e80 V:[UIScrollView:0x9e84b00]-(0)-[_UILayoutGuide:0xb862070]>

I'm able to remove this conflict if instead of

     self.view = originalView;
     [self.view addSubview:_scrollView];

I do

     [self.view = _scrollView];

but that doesn't result in the view hierarchy I want.

1
Did your solution work in the end?Konstantine Kalbazov

1 Answers

3
votes
  1. If you need load ScrollView like Root View for ViewController you need override loadView method without call super like this:

    -(void)loadView { _scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero]; [self setView:self.scrollView]; }

  2. Constraints didn't work with AutoresizeMask. If you add constraints to view, this view will be need set [view setAutoresizingMask:NO]; or you catch conflict.

  3. Add subviews before then setup constraints.