2013-10-12 48 views
1

我想要在屏幕頂部固定一些視圖,其他一些視圖在底部,以及在頂部視圖和底部視圖之間等距離的單個固定大小視圖。iOS Autolayout保持距離2個視圖

我無法弄清楚如何用Autolayout約束來做到這一點。我是否需要向用戶界面添加一些間隔視圖,或以編程方式計算所需的位置?

enter image description here

回答

1

你可以只用一個附加視圖做到這一點。它會是這樣的:

stuff_on_top 
middle_view (with fixed size view inside) 
stuff_on_bottom 

有會是stuff_on_top & middle_view之間和middle_view之間& stuff_on_bottom垂直間距限制。 fixed size view將在middle_view中水平和垂直居中。

這樣做,這將是兩者的另一種方式把兩個間隔的觀點:stuff_on_top之間& middle_viewmiddle_view之間& stuff_on_bottom。然後你會添加一個間距視圖高度相等的約束。

+0

你的意思是,'middle_view'將頂部之間擴展和底部視圖,因爲垂直間距約束? – Axarydax

+0

沒錯。 'stuff_on_top'和'stuff_on_bottom'將具有固定大小。自動佈局也會知道屏幕的高度,所以在計算'middle_view'的高度時沒有問題。 –

1

退房這一類:https://github.com/jrturton/UIView-Autolayout

然後,你可以做這樣簡單的東西...

#import "UIView+AutoLayout.h" 

... 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIView *topView = [UIView autoLayoutView]; 
    UIView *centralContainerView = [UIView autoLayoutView]; 
    UIView *centralView = [UIView autoLayoutView]; 
    UIView *bottomView = [UIView autoLayoutView]; 

    topView.backgroundColor = [UIColor redColor]; 
    bottomView.backgroundColor = [UIColor redColor]; 
    centralView.backgroundColor = [UIColor greenColor]; 

    [self.view addSubview:topView]; 
    [self.view addSubview:centralContainerView]; 
    [centralContainerView addSubview:centralView]; 
    [self.view addSubview:bottomView]; 


    //Pins the topView to the top, left and right edges of its superview (in iOS 7, it uses the topLayoutGuide) 
    [topView pinToSuperviewEdges:JRTViewPinTopEdge|JRTViewPinLeftEdge|JRTViewPinRightEdge inset:0 usingLayoutGuidesFrom:self]; 
    //Constrains the height of topView to 75pts (if a value is passed as zero, no constrain is applied to that axis) 
    [topView constrainToSize:CGSizeMake(0, 75)]; 

    //Pins the centralContainerView to the left and right edges of its superview 
    [centralContainerView pinToSuperviewEdges:JRTViewPinLeftEdge|JRTViewPinRightEdge inset:0]; 
    //Pins the top of centralContainerView to the bottom of topView 
    [centralContainerView pinEdge:NSLayoutAttributeTop toEdge:NSLayoutAttributeBottom ofItem:topView]; 
    //Pins the bottom of centralContainerView to the top of bottomView 
    [centralContainerView pinEdge:NSLayoutAttributeBottom toEdge:NSLayoutAttributeTop ofItem:bottomView]; 

    //Centers centralView on the Y axis of its superview 
    [centralView centerInContainerOnAxis:NSLayoutAttributeCenterY]; 
    //Pins the centralView to the left and right edges of its superview 
    [centralView pinToSuperviewEdges:JRTViewPinLeftEdge|JRTViewPinRightEdge inset:0]; 
    //Constrains the height of topView to 100pts 
    [centralView constrainToSize:CGSizeMake(0, 100)]; 

    //Pins the topView to the bottom, left and right edges of its superview (in iOS 7, it uses the bottomLayoutGuide) 
    [bottomView pinToSuperviewEdges:JRTViewPinBottomEdge|JRTViewPinLeftEdge|JRTViewPinRightEdge inset:0 usingLayoutGuidesFrom:self]; 
    //Constrains the height of topView to 75pts 
    [bottomView constrainToSize:CGSizeMake(0, 75)]; 

} 

,你會得到這樣的輸出:

output on a 4" device

編輯:

我沒有看到接口生成器標籤,只是跳到結論...一個接口生成器的替代方法將工作類似於上述..你需要有三個主視圖,一個固定到頂部,另一個固定到底部..然後一個靈活的寬度固定到另外兩個視圖。

然後,您可以在中間視圖中將具有固定高度的第四個視圖居中。那麼這會給你你正在尋找