子视图高度调整大小

时间:2014-11-11 21:02:43

标签: xcode uiview hidden

我有一个带有2个子视图的UIView。一次只能看到1个子视图,由分段控件控制。子视图具有不同的高度。如何将UIView高度设置为依赖于可见子视图?

编辑 - 下面的代码适用于分段控件的初始来回segue,但后来保持在30px。调试器显示存在满足约束的问题,但我不确定如何纠正它。

错误:

  

无法同时满足约束条件。       可能至少下列列表中的一个约束是您不想要的约束。试试这个:(1)看看每个约束并试着找出你不期望的东西; (2)找到添加了不需要的约束或约束的代码并修复它。 (注意:如果你看到你不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性的文档translatesAutoresizingMaskIntoConstraints)

("<NSLayoutConstraint:0x7f84f84b3b80 V:[UIView:0x7f84f852bc60(60)]>",
"<NSLayoutConstraint:0x7f84f8494d20 V:[UIView:0x7f84f852bc60(30)]>")

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f84f84b3b80 V:[UIView:0x7f84f852bc60(60)]>

代码:

- (IBAction)segmentValueChanged:(UISegmentedControl *)sender {
switch (sender.selectedSegmentIndex) {
    case 0:
        [_ParentView setTranslatesAutoresizingMaskIntoConstraints:NO];
        [_ParentView addConstraint:[NSLayoutConstraint
                                   constraintWithItem:_ParentView
                                   attribute:NSLayoutAttributeHeight
                                   relatedBy:NSLayoutRelationEqual
                                   toItem:nil
                                   attribute:NSLayoutAttributeNotAnAttribute
                                   multiplier:1.0
                                   constant:30.0]];

        self.1stView.hidden = NO;
        self.2ndView.hidden = YES;
        break;
    case 1:
        [_ParentView setTranslatesAutoresizingMaskIntoConstraints:NO];
        [_ParentView addConstraint:[NSLayoutConstraint
                                  constraintWithItem:_ParentView
                                  attribute:NSLayoutAttributeHeight
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:nil
                                  attribute:NSLayoutAttributeNotAnAttribute
                                  multiplier:1.0
                                  constant:60.0]];

        self.1stView.hidden = YES;
        self.2ndView.hidden = NO;
        break;
    default:
        break;
    }
}

由于

2 个答案:

答案 0 :(得分:1)

好的,我在故事板中进行了设置并让它发挥作用。

所以你有三个故事板的UIViews

你的.h文件中的

@property (nonatomic, retain) IBOutlet UIView *parentView;
@property (nonatomic, retain) IBOutlet UIView *HDDView;
@property (nonatomic, retain) IBOutlet UIView *SSDView;

然后在您的.m文件中

@synthesize parentView, HDDView, SSDView;

- (void)viewDidLoad {

    //so here you can set which uiview is hidden when the view loads
    //or you probably did it in your storyboard, either way works
    self.SSDView.hidden = YES;

}

- (IBAction)segmentValueChanged:(UISegmentedControl *)sender {
 switch (sender.selectedSegmentIndex) {
     case 0:
         self.HDDView.hidden = NO;
         self.SSDView.hidden = YES;


         CGRect newFrame = parentView.frame;
         newFrame.size.height = HDDView.frame.size.height;
         parentView.frame = newFrame;

         break;
     case 1:
         self.HDDView.hidden = YES;
         self.SSDView.hidden = NO;

         CGRect otherFrame = parentView.frame;
         otherFrame.size.height = SSDView.frame.size.height;
         parentView.frame = otherFrame;

         break;
     default:
     break;
     }
 }

现在在你的故事板中添加你的SSDView和HDDView UIViews并将它们连接到他们的IBOutlets。然后添加你的parentView UIView并将其挂钩。然后在parentView UIView中拖动SSDView和HDDView。 **确保将SSDView和HDDView放在parentView的顶部,以便每个UIView的顶部边界都是齐平的。还要确保没有将其中一个子视图拖动到另一个子视图中。最后为parentView添加一些约束,使其处于您想要的位置。然后连接你的分段控制器,你就可以了。

总而言之,我建议不要将Interface Builder用于这样复杂的事情。您可以使用代码执行此操作,它可以更加自定义,更容易调整。我开始使用IB,但以编程方式执行这些操作要好得多,因为您可以拥有更多控制权。我希望这会帮助你。

答案 1 :(得分:0)

它可能不是最干净但我能够得到以下代码才能工作。感谢@AJB的帮助。

- (IBAction)segmentValueChanged:(UISegmentedControl *)sender {
switch (sender.selectedSegmentIndex) {
    case 0:
        self.1stView.hidden = NO;
        self.2ndView.hidden = YES;

        _1stconstraint = [NSLayoutConstraint
                       constraintWithItem:_ParentView
                       attribute:NSLayoutAttributeHeight
                       relatedBy:NSLayoutRelationEqual
                       toItem:nil
                       attribute:NSLayoutAttributeNotAnAttribute
                       multiplier:1.0
                       constant:30.0];

        [_ParentView setTranslatesAutoresizingMaskIntoConstraints:NO];

        [_ParentView removeConstraint:_2ndconstraint];
        [_ParentView addConstraint:_1stconstraint];

        break;
    case 1:
        self.1stView.hidden = YES;
        self.2ndView.hidden = NO;

        _2ndconstraint = [NSLayoutConstraint
                                  constraintWithItem:_ParentView
                                  attribute:NSLayoutAttributeHeight
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:nil
                                  attribute:NSLayoutAttributeNotAnAttribute
                                  multiplier:1.0
                                  constant:60.0];

        [_ParentView setTranslatesAutoresizingMaskIntoConstraints:NO];

        [_ParentView removeConstraint:_1stconstraint];
        [_ParentView addConstraint:_2ndconstraint];

        break;
    default:
        break;
    }
}
相关问题