我將一個AdBannerView
帶到我的應用程序的底部,並且由於使用共享的橫幅(而不是在Storyboard
中創建它)而以編程方式創建。我的應用在縱向和橫向模式下工作,所以在我的viewWillAppear
中,我設置了AdBannerView
的框架,我也嘗試了第一次以編程方式使用AutoLayout
,但我遇到了一些問題。以編程方式設置NSLayoutAttributeLeading空間的一些警告
我創造了AdBannerView
在Storyboard
測試和管理,適用於AdBannerView
,所以當我旋轉的裝置,它會出現在橫向和縱向底部Leading Spaces to Superview
,Trailing Spaces to Superview
和Bottom 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兼容。
任何有關這方面的指導真的很感激。
它顯示你給領先,尾隨和底部空間....但我認爲你還需要添加一個視圖的高度來擺脫這... –