如果我添加任何类型的 UIDynamicBehavior
在我看来,当设备旋转时,它会完全破坏事物。这是它的纵向(正确显示):
这里是横向的,全部坏了:
我不相信这是自动布局问题,因为如果我删除添加 UIDynamicBehavior
的调用它工作正常,没有自动布局问题。也不会抛出自动布局错误。这是代码:
@interface SWViewController () {
UICollisionBehavior *coll;
UIDynamicAnimator *dynamicAnimator;
}
@implementation SWViewController
- (void)viewDidLoad {
[super viewDidLoad];
dynamicAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self setupCollisions]; // commenting this out fixes the layout
}
- (void)setupCollisions {
NSArray *dynamicViews = @[greenView];
coll = [[UICollisionBehavior alloc] initWithItems:dynamicViews];
CGFloat topBound = CGRectGetMinY(greenView.frame);
[coll addBoundaryWithIdentifier:@"top"
fromPoint:CGPointMake(0, h1)
toPoint:CGPointMake(CGRectGetWidth(greenView.frame), h1)];
[dynamicAnimator addBehavior:coll];
}
如果我覆盖
didRotateFromInterfaceOrientation
我可以看到 greenView
的顶部边界不遵循自动布局所说的它应该(同样,删除对 setupCollisions
的调用修复了这个问题)。greenView
上的自动布局边界是:height = 200
trailing space to Superview = 0
leading space to Superview = 0
bottom space to Superview = 0
最佳答案
我发现的一种解决方案是覆盖 willRotateToInterfaceOrientation:
并删除 UIDynamicItems
然后在 didRotateFromInterfaceOrientation:
中重新添加它们.这让我觉得相当笨拙,并且一旦我添加了更复杂的行为,就可能会引入错误。
关于ios7 - UIDynamics 和设备旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19059538/