我将AdBannerView
带到我的应用程序底部,由于使用了共享横幅(而不是在Storyboard
中创建),我正在以编程方式创建它。我的应用程序在纵向和横向模式下工作,因此在我的viewWillAppear
中,我正在为AdBannerView
设置框架,我也第一次尝试以编程方式使用AutoLayout
,但我遇到了一些问题。
我在Storyboard
中创建了AdBannerView
作为测试,并设法在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。
首先,您需要调用[self.adBanner setTranslatesAutoresizingMasksIntoConstraints:NO]
其次,对于adBanner
视图,仍然需要顶部偏移约束或高度约束。
此外,由于您使用的是自动布局,您不需要再设置框架,这将由布局系统自动完成。
如果您需要更多帮助,请告诉我:)
您仍然需要高度约束。你可以使用类似的东西:
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[view(==50)]" options:0 metrics:nil views:@{ @"view": self.adBanner }]];
设置完所有约束后,可以调用[self.view layoutIfNeeded]
。在方法调用之后,打印出adBanner
的框架,用于调试目的,以确保一切都是您想要的。