【问题标题】:iOS7 Storyboard how to specify constraints to center a view for both landscape and portrait?iOS7故事板如何指定约束以使横向和纵向视图居中?
【发布时间】:2014-08-06 12:11:21
【问题描述】:

我正在尝试使用故事板将 UIView 居中用于横向和纵向。下面是为此目的工作的旧弹簧/支柱。下面还有我定义的约束。我的视图在横向模式下未居中显示。 横向和纵向模式的视图居中缺少什么约束?

【问题讨论】:

    标签: iphone ios7 uiview autolayout uistoryboard


    【解决方案1】:

    为视图(View1)添加约束以使其水平和垂直居中对齐其superView,并为视图(View1)添加宽度和高度约束,并为添加的宽度和高度约束创建出口。

    @interface
    
    @property (strong, nonatomic) IBOutlet NSLayoutConstraint *widthConstraint;
    @property (strong, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
    
    @end
    

    更新约束:

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    
        if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
        self.widthConstraint.constant = ???; //calculated Values
        self.heightConstraint.constant = ????  ;
    }
    else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    {
        self.widthConstraint.constant = ???;
        self.heightConstraint.constant = ????;
    }
    
     [self.view layoutIfNeeeded];
    }
    

    【讨论】:

      【解决方案2】:

      您可以将属性与约束相关联,并在方向更改时更改其值

      @interface
      @property (strong, nonatomic) IBOutlet NSLayoutConstraint *leftMarginConstraint;
      @property (strong, nonatomic) IBOutlet NSLayoutConstraint *topMarginConstraint;
      @end
      
      
      @implementation
      
      - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
      
          if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
      { 
          leftMarginConstraint.constant = ???
          topMarginConstraint.constant = ????  
      }
      else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) 
      {
         leftMarginConstraint.constant = ???
          topMarginConstraint.constant = ????  
      }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-02
        • 2017-05-28
        • 1970-01-01
        • 2015-02-20
        • 1970-01-01
        相关资源
        最近更新 更多