iOs - 将uiview插入到代码中的另一个视图中,保持约束

时间:2014-05-30 15:33:15

标签: ios uiview uiscrollview autolayout nslayoutconstraint

我想要实现的目标。 每当我有一个继承自我的类SuperViewController的UIViewController时,在我的superview控制器的viewDidLoad中,我获取我的UIViewController的所有子视图,创建一个UIScrollView,将所有子视图添加到它并将此UIScrollView设置为UIViewController的唯一子视图。 / p>

调用[super viewDidLoad];

之前的UIViewController结构
UIViewController
  UIView
     subview1
     subview2
     subview3

UIViewController
  UIView
    UIScrollView
       subview1
       subview2
       subview3

使用与UIView相关的约束 - 子视图,移至UIScrollView-subviews

我这样做的原因是因为在我们的应用程序中我们有很多处理scrollViews而我只想编写代码在超类中添加一次并让它启用/禁用基于的滚动内容大小。

我目前在superviewController中的内容(简化)

 - (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];




 }

这给了我不明白的约束冲突

 (
"<_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]>

如果不是

,我可以删除此冲突
     self.view = originalView;
     [self.view addSubview:_scrollView];

我做

     [self.view = _scrollView];

但这并不会产生我想要的视图层次结构。

1 个答案:

答案 0 :(得分:3)

  1. 如果您需要像ViewController的Root View一样加载ScrollView,您需要覆盖loadView方法,而不需要像这样调用super:

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

  2. 约束无法与AutoresizeMask一起使用。如果您向视图添加约束,则此视图将需要设置为[view setAutoresizingMask:NO];,否则您将发现冲突。

  3. 在设置约束之前添加子视图。