动态加载XIB的自动布局问题

时间:2015-06-20 07:57:40

标签: ios objective-c uiscrollview xcode6 autolayout

我遇到了自动布局和我正在尝试构建的特定UI流程的问题。

基本上,我有以下内容:

  • 故事板
  • 名为xib
  • UpdateYou.xib
  • 名为xib
  • UpdateFilter.xib

storyboard内,UIViewController被称为UpdateViewController的场景,其中UISegmentedControl有两个选项和滚动视图。

一般的前提是当UISegmentedControl在Option1中时,它会将UpdateYou.xib加载到滚动视图中。这受限于父视图的宽度,并根据需要垂直展开。

但是,当选择UISegmentedControl的Option2时,会加载UpdateFilter xib,但不会将其限制为父视图的宽度。

下面是约束的打印(第一个***分隔的日志用于正确布局的视图):

2015-06-20 08:39:27.514 [42981:1543261] *************************************************
Constraint: <NSLayoutConstraint:0x7fc691d7eeb0 UIView:0x7fc691d7d720.leading == UIView:0x7fc691d7d5f0.leadingMargin - 16>
Constraint: <NSLayoutConstraint:0x7fc691d7ef00 UIView:0x7fc691d7d720.top == UIView:0x7fc691d7d5f0.topMargin>
Constraint: <NSLayoutConstraint:0x7fc691d7ef50 UIView:0x7fc691d7d5f0.trailingMargin == UIView:0x7fc691d7d720.trailing - 16>
Constraint: <NSLayoutConstraint:0x7fc691d7efa0 UIView:0x7fc691d7d5f0.trailingMargin == UIView:0x7fc691d7d900.trailing - 16>
Constraint: <NSLayoutConstraint:0x7fc691d7eff0 V:[_UILayoutGuide:0x7fc691d7e4a0]-(0)-[UIView:0x7fc691d7d900]>
Constraint: <NSLayoutConstraint:0x7fc691d7f040 UIView:0x7fc691d7d900.leading == UIView:0x7fc691d7d5f0.leadingMargin - 16>
Constraint: <NSLayoutConstraint:0x7fc691d7f090 UISegmentedControl:0x7fc691d018b0.trailing == UIView:0x7fc691d7d5f0.trailingMargin>
Constraint: <NSLayoutConstraint:0x7fc691d7f0e0 V:[UIView:0x7fc691d7d900]-(8)-[UISegmentedControl:0x7fc691d018b0]>
Constraint: <NSLayoutConstraint:0x7fc691d7f130 UISegmentedControl:0x7fc691d018b0.leading == UIView:0x7fc691d7d5f0.leadingMargin>
Constraint: <NSLayoutConstraint:0x7fc691d7f180 UIView:0x7fc691d7d5f0.trailingMargin == UIScrollView:0x7fc691d7bbd0.trailing - 16>
Constraint: <NSLayoutConstraint:0x7fc691d7f1d0 UIScrollView:0x7fc691d7bbd0.leading == UIView:0x7fc691d7d5f0.leadingMargin - 16>
Constraint: <NSLayoutConstraint:0x7fc691d7f220 V:[UISegmentedControl:0x7fc691d018b0]-(8)-[UIScrollView:0x7fc691d7bbd0]>
Constraint: <NSLayoutConstraint:0x7fc691d7f270 V:[UIScrollView:0x7fc691d7bbd0]-(0)-[_UILayoutGuide:0x7fc691d7e9f0]>
Constraint: <_UILayoutSupportConstraint:0x7fc691d51190 V:[_UILayoutGuide:0x7fc691d7e4a0(0)]>
Constraint: <_UILayoutSupportConstraint:0x7fc691d55c60 V:|-(0)-[_UILayoutGuide:0x7fc691d7e4a0]   (Names: '|':UIView:0x7fc691d7d5f0 )>
Constraint: <_UILayoutSupportConstraint:0x7fc691d4f9a0 V:[_UILayoutGuide:0x7fc691d7e9f0(0)]>
Constraint: <_UILayoutSupportConstraint:0x7fc691d66220 _UILayoutGuide:0x7fc691d7e9f0.bottom == UIView:0x7fc691d7d5f0.bottom>
*************************************************

*************************************************
Constraint: <NSLayoutConstraint:0x7fc691d7eeb0 UIView:0x7fc691d7d720.leading == UIView:0x7fc691d7d5f0.leadingMargin - 16>
Constraint: <NSLayoutConstraint:0x7fc691d7ef00 UIView:0x7fc691d7d720.top == UIView:0x7fc691d7d5f0.topMargin>
Constraint: <NSLayoutConstraint:0x7fc691d7ef50 UIView:0x7fc691d7d5f0.trailingMargin == UIView:0x7fc691d7d720.trailing - 16>
Constraint: <NSLayoutConstraint:0x7fc691d7efa0 UIView:0x7fc691d7d5f0.trailingMargin == UIView:0x7fc691d7d900.trailing - 16>
Constraint: <NSLayoutConstraint:0x7fc691d7eff0 V:[_UILayoutGuide:0x7fc691d7e4a0]-(0)-[UIView:0x7fc691d7d900]>
Constraint: <NSLayoutConstraint:0x7fc691d7f040 UIView:0x7fc691d7d900.leading == UIView:0x7fc691d7d5f0.leadingMargin - 16>
Constraint: <NSLayoutConstraint:0x7fc691d7f090 UISegmentedControl:0x7fc691d018b0.trailing == UIView:0x7fc691d7d5f0.trailingMargin>
Constraint: <NSLayoutConstraint:0x7fc691d7f0e0 V:[UIView:0x7fc691d7d900]-(8)-[UISegmentedControl:0x7fc691d018b0]>
Constraint: <NSLayoutConstraint:0x7fc691d7f130 UISegmentedControl:0x7fc691d018b0.leading == UIView:0x7fc691d7d5f0.leadingMargin>
Constraint: <NSLayoutConstraint:0x7fc691d7f180 UIView:0x7fc691d7d5f0.trailingMargin == UIScrollView:0x7fc691d7bbd0.trailing - 16>
Constraint: <NSLayoutConstraint:0x7fc691d7f1d0 UIScrollView:0x7fc691d7bbd0.leading == UIView:0x7fc691d7d5f0.leadingMargin - 16>
Constraint: <NSLayoutConstraint:0x7fc691d7f220 V:[UISegmentedControl:0x7fc691d018b0]-(8)-[UIScrollView:0x7fc691d7bbd0]>
Constraint: <NSLayoutConstraint:0x7fc691d7f270 V:[UIScrollView:0x7fc691d7bbd0]-(0)-[_UILayoutGuide:0x7fc691d7e9f0]>
Constraint: <_UILayoutSupportConstraint:0x7fc691d51190 V:[_UILayoutGuide:0x7fc691d7e4a0(20)]>
Constraint: <_UILayoutSupportConstraint:0x7fc691d55c60 V:|-(0)-[_UILayoutGuide:0x7fc691d7e4a0]   (Names: '|':UIView:0x7fc691d7d5f0 )>
Constraint: <_UILayoutSupportConstraint:0x7fc691d4f9a0 V:[_UILayoutGuide:0x7fc691d7e9f0(0)]>
Constraint: <_UILayoutSupportConstraint:0x7fc691d66220 _UILayoutGuide:0x7fc691d7e9f0.bottom == UIView:0x7fc691d7d5f0.bottom>
Constraint: <NSLayoutConstraint:0x7fc69402d9e0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fc691d7d5f0(375)]>
Constraint: <NSLayoutConstraint:0x7fc69402ed70 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7fc691d7d5f0(667)]>
Constraint: <NSAutoresizingMaskLayoutConstraint:0x7fc69402db80 h=-&- v=-&- 'UIView-Encapsulated-Layout-Left' H:|-(0)-[UIView:0x7fc691d7d5f0]   (Names: '|':UIView:0x7fc691c40020 )>
Constraint: <NSAutoresizingMaskLayoutConstraint:0x7fc69402d4e0 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UIView:0x7fc691d7d5f0]   (Names: '|':UIView:0x7fc691c40020 )>
*************************************************

我可以看到问题在于第二个视图中包含的附加约束,但我不知道为什么他们在那里或他们如何到达那里。我已经检查了IB中的约束,但它们不包括在内。

我试图在违规视图上将translatesAutoresizingMaskIntoConstraints设置为NO,但这会让布局更糟糕!

通过以下代码添加XIB:

- (IBAction)onSegmentedControlValueChanged:(id)sender
{
    switch (self.segmentedControl.selectedSegmentIndex) {
    case 0:
        if (!updateProfileYou) {
            updateProfileYou = [[[NSBundle mainBundle] loadNibNamed:@"UpdateProfileYou" owner:self options:nil] lastObject];
        }
        [self.scrollview.subviews[0] removeFromSuperview];
        [self.scrollview addSubview:updateProfileYou];
        [self.scrollview setContentSize:CGSizeMake(320, updateProfileYou.frame.size.height)];
        break;
    case 1:
        if (!updateProfileFilters) {
            updateProfileFilters = [[[NSBundle mainBundle] loadNibNamed:@"UpdateProfileFilters" owner:self options:nil] lastObject];
        }
        [self.scrollview.subviews[0] removeFromSuperview];
        [self.scrollview addSubview:updateProfileFilters];
        [self.scrollview setContentSize:CGSizeMake(320, updateProfileFilters.frame.size.height)];
        break;
    default:
        break;
    }
}

由于

0 个答案:

没有答案