在我解雇一个呈现的视图控制器之后,我试图理解我的视图布局会发生什么。任何人都能解释为什么我的布局在以下场景中断开: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