2015-04-17 74 views
0

我將一個AdBannerView帶到我的應用程序的底部,並且由於使用共享的橫幅(而不是在Storyboard中創建它)而以編程方式創建。我的應用在縱向和橫向模式下工作,所以在我的viewWillAppear中,我設置了AdBannerView的框架,我也嘗試了第一次以編程方式使用AutoLayout,但我遇到了一些問題。以編程方式設置NSLayoutAttributeLeading空間的一些警告

我創造了AdBannerViewStoryboard測試和管理,適用於AdBannerView,所以當我旋轉的裝置,它會出現在橫向和縱向底部Leading Spaces to SuperviewTrailing Spaces to SuperviewBottom Space to Superview約束。

但是,當以編程方式執行AutoLayout時,我得不到相同的結果。

這裏是我的代碼:

[self.adBanner setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-100, 320, 50)]; 
    [self.view addSubview:self.adBanner]; 

    NSLayoutConstraint *myConstraint =[NSLayoutConstraint 
             constraintWithItem:self.adBanner 
             attribute:NSLayoutAttributeLeading 
             relatedBy:NSLayoutRelationEqual 
             toItem:self.view 
             attribute:NSLayoutAttributeTrailing 
             multiplier:1.0 
             constant:0]; 

    [self.view addConstraint:myConstraint]; 



    myConstraint =[NSLayoutConstraint constraintWithItem:self.adBanner 
               attribute:NSLayoutAttributeTrailing 
               relatedBy:NSLayoutRelationEqual 
                toItem:self.view 
               attribute:NSLayoutAttributeLeading 
               multiplier:1 
               constant:0]; 

    [self.view addConstraint:myConstraint]; 

    myConstraint =[NSLayoutConstraint constraintWithItem:self.adBanner 
               attribute:NSLayoutAttributeBottom 
               relatedBy:NSLayoutRelationEqual 
                toItem:self.view 
               attribute:NSLayoutAttributeBaseline 
               multiplier:1 
               constant:0]; 

    [self.view addConstraint:myConstraint]; 

什麼我得到的控制檯:

Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7fcb017020f0 ADBannerView:0x7fcb014c1860.bottom == UIView:0x7fcb014f9f70.lastBaseline>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7fcb01624280 h=--& v=--& ADBannerView:0x7fcb014c1860.midY == + 957>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7fcb016242f0 h=--& v=--& V:[ADBannerView:0x7fcb014c1860(66)]>", 
    "<NSLayoutConstraint:0x7fcb017039b0 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7fcb014f9f70(768)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7fcb014fa8a0 h=--& v=--& 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UIView:0x7fcb014f9f70] (Names: '|':UIViewControllerWrapperView:0x7fcb01619da0)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fcb017020f0 ADBannerView:0x7fcb014c1860.bottom == UIView:0x7fcb014f9f70.lastBaseline> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

設備旋轉,但在風景模式下,AdBannerView不顯示,可能是因爲它有已被移出屏幕。

我已經通過編程方式在AutoLayout上完成了很多教程,我老實說就是不明白。

問:

有沒有一種方法可以讓我腳我AdBannerView屏幕(領導)的左邊緣到屏幕(追蹤)的右邊緣到屏幕的底部(下圖)以編程方式,所以當我旋轉我的iPhone/iPad時,AdBannerView會在那裏顯示?

這個應用程序是iOS 7和iOS 8兼容。

任何有關這方面的指導真的很感激。

+0

它顯示你給領先,尾隨和底部空間....但我認爲你還需要添加一個視圖的高度來擺脫這... –

回答

4

首先,你需要調用​​

其次,你仍然需要使用一個頂偏約束或爲adBanner視圖的高度限制。

此外,由於您正在使用自動佈局,因此無需再設置框架,這將由佈局系統自動完成。

讓我知道如果你需要更多的幫助:)


你仍然需要高度約束。您可以使用類似:

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[view(==50)]" options:0 metrics:nil views:@{ @"view": self.adBanner }]]; 

您可以撥打[self.view layoutIfNeeded]你已經安裝所有的約束條件之後。爲方便調試,在方法調用之後打印adBanner的幀,以確保所有內容都是您想要的。

+1

這是現貨。第一部分甚至在上面的控制檯輸出中:'如果你看到'你不明白的'NSAutoresizingMaskLayoutConstraints',請參考'UIView'屬性'translatesAutoresizingMaskIntoConstraints''的文檔。 –

+0

做完這個答案中提到的所有事情之後,你仍然似乎試圖將super的前沿連接到橫幅的尾部,並將super的尾部連接到橫幅的前面。我想邏輯上這應該橫向翻轉橫幅,但這可能不是預期的效果(並且不會工作)。在 –

+0

以上的前2個約束中反轉toItem屬性的前導屬性和尾隨屬性非常感謝您的答案和後續答案。我非常感謝!我有這個工作,但我想確定我在做什麼實際上是正確的 - 所以我已經在setFrame之後將setTranslatesAutoresizingMaskIntroConstraints添加到NO。如果我刪除了setFrame,則橫幅出現在頂部,我將第一個約束保持爲領先,第二個爲尾部,第三個爲底部,並且工作正常。我是否還需要添加高度/頂部約束?它看起來像我想象的那樣,隨着旋轉等等。 – amitsbajaj