我试图了解关闭呈现的视图控制器后视图布局会发生什么。任何人都能够解释为什么我的布局在以下情况下会中断: 1 个包含 1 个子视图控制器的主视图控制器。主视图控制器成功呈现了第三个视图控制器,但是在关闭呈现的 vc 后,子视图控制器已经改变了它的布局。这是演示我的困境的示例 RubyMotion 代码:
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
@presenting = false
window.rootViewController = main_vc
main_vc.addChildViewController(content_vc)
main_vc.view.addSubview(content_vc.view)
constraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|[content]|",
options: NSLayoutAttributeLeft,
metrics: nil,
views: { 'content' => content_vc.view })
window.addConstraints(constraints)
constraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|[content]|",
options: NSLayoutAttributeTop,
metrics: nil,
views: { 'content' => content_vc.view })
window.addConstraints(constraints)
[ content_vc.view.subviews.first ].each do |item|
main_vc.view.addConstraint(NSLayoutConstraint.constraintWithItem(item,
attribute: NSLayoutAttributeCenterX,
relatedBy: NSLayoutRelationEqual,
toItem: main_vc.view,
attribute: NSLayoutAttributeCenterX,
multiplier: 1, constant: 0))
main_vc.view.addConstraint(NSLayoutConstraint.constraintWithItem(item,
attribute: NSLayoutAttributeCenterY,
relatedBy: NSLayoutRelationEqual,
toItem: main_vc.view,
attribute: NSLayoutAttributeCenterY,
multiplier: 1, constant: -40))
end
brand_vc.view.when_tapped do
puts "tapped brand_vc"
main_vc.dismissViewControllerAnimated(true, completion: lambda { })
end
content_vc.view.when_tapped do
puts "tapped content_vc"
main_vc.presentViewController(brand_vc, animated: true, completion: lambda { })
end
true
end
def window
@window ||= UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds).tap do |w|
w.backgroundColor = UIColor.greenColor
w.makeKeyAndVisible
end
end
def main_vc
@main_vc ||= UIViewController.alloc.initWithNibName(nil, bundle: nil).tap do |vc|
vc.view.translatesAutoresizingMaskIntoConstraints = false
vc.view.backgroundColor = UIColor.redColor
vc.view.addSubview(MyLabel.alloc.init_with_text("Main VC - (red color)"))
end
end
def content_vc
@content_vc ||= UIViewController.alloc.initWithNibName(nil, bundle: nil).tap do |vc|
vc.view.translatesAutoresizingMaskIntoConstraints = false
vc.view.backgroundColor = UIColor.blueColor
vc.view.addSubview(MyLabel.alloc.init_with_text("Content VC (blue color)"))
end
end
def brand_vc
@brand_vc ||= UIViewController.alloc.initWithNibName(nil, bundle: nil).tap do |vc|
vc.view.translatesAutoresizingMaskIntoConstraints = false
vc.view.backgroundColor = UIColor.yellowColor
end
end
end
class MyLabel < UILabel
def init_with_text(str)
self.init
self.translatesAutoresizingMaskIntoConstraints = false
self.text = str
self
end
end