ios7 - UIDynamics 和设备旋转

标签 ios7 autolayout uikit-dynamics uicollisionbehavior uidynamicbehavior

如果我添加任何类型的 UIDynamicBehavior在我看来,当设备旋转时,它会完全破坏事物。这是它的纵向(正确显示):

Correct Layout

这里是横向的,全部坏了:Broken layout

我不相信这是自动布局问题,因为如果我删除添加 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/

相关文章:

html - 如何获得 HTML 属性文本的真实高度

ios - 如何在可移动的两个UIView之间重新绘制连线

ios - - [NSDateComponents week] 已弃用,哪个枚举提供与以前相同的行为?

ios - 验证时出现 Xcode 错误 - "Your binary is not optimized for iPhone 5"

ios - TableView 行分隔符就像在 iOS7 中的通知中心

ios - UIKit 动态 : recognize rounded Shapes and Boundaries

ios - 将 CGAffineTransformScale 与 UIAttachmentBehavior (UIDynamicAnimator) 结合使用

ios - 在滑动状态栏的 map 应用中再现苹果效果

autolayout - macOS 应用程序中 NSTableView 行中具有可变高度的可编辑 NSTextField

ios - 使用自动布局调整 xib 元素的宽度