我将 AdBannerView
带到我的应用程序的底部,并且由于使用了共享横幅(而不是在 Storyboard
中创建它),所以我以编程方式创建它.我的应用程序在纵向和横向模式下工作,因此在我的 viewWillAppear
中,我正在为 AdBannerView
设置框架,我也第一次尝试使用 AutoLayout
以编程方式,但我遇到了一些问题。
我在 Storyboard
中创建了 AdBannerView
作为测试,并设法将 Leading Spaces 应用于 Superview
,Trailing Spaces 应用于 Superview
和 Bottom Space to Superview
对 AdBannerView
的限制,所以当我旋转设备时,它会在横向和纵向显示在底部。
但是,当以编程方式执行 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
固定到屏幕的左边缘(Leading)、屏幕的右边缘(Trailing)和屏幕的底部(Bottom)以编程方式,以便当我旋转我的 iPhone/iPad 时,AdBannerView 会显示在那里?
此应用兼容 iOS 7 和 iOS 8。
最佳答案
首先,您需要调用[self.adBanner setTranslatesAutoresizingMasksIntoConstraints:NO]
其次,对于 adBanner
View ,您仍然需要顶部偏移约束或高度约束。
此外,由于您使用的是自动布局,因此您不再需要设置框架,这将由布局系统自动完成。
如果您需要更多帮助,请告诉我:)
您仍然需要高度限制。你可以使用类似的东西:
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[view(==50)]" options:0 metrics:nil views:@{ @"view": self.adBanner }]];
您可以在设置所有约束后调用[self.view layoutIfNeeded]
。在方法调用之后打印出 adBanner
的框架,用于调试目的,以确保一切都是您想要的。
关于ios - 以编程方式将 NSLayoutAttributeLeading Space 设置为 Superview 并带有一些警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29694632/