Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ipad 使用故事板和自动布局固定纵向和横向视图宽度?_Ipad_Cocoa Touch_Ios6_Storyboard_Autolayout - Fatal编程技术网

Ipad 使用故事板和自动布局固定纵向和横向视图宽度?

Ipad 使用故事板和自动布局固定纵向和横向视图宽度?,ipad,cocoa-touch,ios6,storyboard,autolayout,Ipad,Cocoa Touch,Ios6,Storyboard,Autolayout,我正在开发这个iPad应用程序,我正在考虑使用自动布局让我的生活更轻松。我创建了这个侧栏控件,允许用户切换到不同的页面(就像标签栏控制器一样,但它位于iPad的左侧)。现在在横向,我希望这个视图的宽度是256像素,但是当iPad在纵向,我希望这个视图的宽度是100像素。如何根据界面方向使用自动布局来固定视图的宽度?您可以使用updateViewConstraints调用在方向更改时修改布局 下面的示例以编程方式创建视图,但您可以在interface builder中连接侧栏的宽度约束以实现相同的

我正在开发这个iPad应用程序,我正在考虑使用自动布局让我的生活更轻松。我创建了这个侧栏控件,允许用户切换到不同的页面(就像标签栏控制器一样,但它位于iPad的左侧)。现在在横向,我希望这个视图的宽度是256像素,但是当iPad在纵向,我希望这个视图的宽度是100像素。如何根据界面方向使用自动布局来固定视图的宽度?

您可以使用updateViewConstraints调用在方向更改时修改布局

下面的示例以编程方式创建视图,但您可以在interface builder中连接侧栏的宽度约束以实现相同的效果

例如:

//create a custom view using autolayout, this is the equivalent of you side bar
- (void)viewDidLoad{
  [super viewDidLoad];
  //create a custom view
  [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
  UIView *vw=[[UIView alloc] init];
  self.customView =vw;
  self.customView.backgroundColor = [UIColor blackColor];
  [self.customView setTranslatesAutoresizingMaskIntoConstraints:NO];
  [self.view addSubview:self.customView];

  NSArray *arr;

  //horizontal constraints
  arr = [NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[vw]"
                                                options:0
                                                metrics:nil
                                                  views:NSDictionaryOfVariableBindings(vw)];
  [self.view addConstraints:arr];

  //vertical constraints
  arr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[vw(200)]"
                                                options:0
                                                metrics:nil
                                                  views:NSDictionaryOfVariableBindings(vw)];
  [self.view addConstraints:arr];

}

- (void)updateViewConstraints{

  [super updateViewConstraints];

  //remove the existing contraint
  if(self.widthConstraint!=nil){
    [self.view removeConstraint:self.widthConstraint];
  }
  //portait set width to 100 
  if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
    self.widthConstraint = [NSLayoutConstraint constraintWithItem:self.customView
                                                        attribute:NSLayoutAttributeWidth
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:nil
                                                        attribute:0
                                                       multiplier:1.0
                                                         constant:100.0];
  }
  //landscape set width to 256
  else{
    self.widthConstraint = [NSLayoutConstraint constraintWithItem:self.customView
                                                        attribute:NSLayoutAttributeWidth
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:nil
                                                        attribute:0
                                                       multiplier:1.0
                                                         constant:256.0];
  }
  [self.view addConstraint:self.widthConstraint];
}

您可以使用updateViewConstraints调用在方向更改时修改布局

下面的示例以编程方式创建视图,但您可以在interface builder中连接侧栏的宽度约束以实现相同的效果

例如:

//create a custom view using autolayout, this is the equivalent of you side bar
- (void)viewDidLoad{
  [super viewDidLoad];
  //create a custom view
  [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
  UIView *vw=[[UIView alloc] init];
  self.customView =vw;
  self.customView.backgroundColor = [UIColor blackColor];
  [self.customView setTranslatesAutoresizingMaskIntoConstraints:NO];
  [self.view addSubview:self.customView];

  NSArray *arr;

  //horizontal constraints
  arr = [NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[vw]"
                                                options:0
                                                metrics:nil
                                                  views:NSDictionaryOfVariableBindings(vw)];
  [self.view addConstraints:arr];

  //vertical constraints
  arr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[vw(200)]"
                                                options:0
                                                metrics:nil
                                                  views:NSDictionaryOfVariableBindings(vw)];
  [self.view addConstraints:arr];

}

- (void)updateViewConstraints{

  [super updateViewConstraints];

  //remove the existing contraint
  if(self.widthConstraint!=nil){
    [self.view removeConstraint:self.widthConstraint];
  }
  //portait set width to 100 
  if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
    self.widthConstraint = [NSLayoutConstraint constraintWithItem:self.customView
                                                        attribute:NSLayoutAttributeWidth
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:nil
                                                        attribute:0
                                                       multiplier:1.0
                                                         constant:100.0];
  }
  //landscape set width to 256
  else{
    self.widthConstraint = [NSLayoutConstraint constraintWithItem:self.customView
                                                        attribute:NSLayoutAttributeWidth
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:nil
                                                        attribute:0
                                                       multiplier:1.0
                                                         constant:256.0];
  }
  [self.view addConstraint:self.widthConstraint];
}

您好,这似乎是可行的,但是,当我第一次旋转时,我收到以下调试器消息:无法同时满足约束。。。(“”,“”)将尝试通过断开约束进行恢复再次添加之前是否删除宽度约束?存在两个相互冲突的固定宽度约束。100和256宽。好的,我把它修好了。而不是调用[self.view removeConstraint:self.widthConstraint];我不得不调用[self.leftColumnContainerView removeConstraint:self.widthConstraint];您好,这似乎是可行的,但是,当我第一次旋转时,我收到以下调试器消息:无法同时满足约束。。。(“”,“”)将尝试通过断开约束进行恢复再次添加之前是否删除宽度约束?存在两个相互冲突的固定宽度约束。100和256宽。好的,我把它修好了。而不是调用[self.view removeConstraint:self.widthConstraint];我不得不调用[self.leftColumnContainerView removeConstraint:self.widthConstraint];