2016-09-19 84 views
2

我正在使用iMessage应用程序,并通过编程添加了一个视图。然而,我似乎无法制定正确的限制,使其始终保持正确的尺寸。例如,如果我将扩展名留给另一个扩展名并返回,视图会下移几百px。我认为这与.isActive有关。我的目标是使视图自动调整大小以始终保持正确的大小或占用全部可用的高度和宽度。以编程方式在swift中添加全尺寸视图

func createBrowser() { 
    let controller = MSStickerBrowserViewController(stickerSize: .small) 
    addChildViewController(controller) 
    view.addSubview(controller.view) 

    controller.view.translatesAutoresizingMaskIntoConstraints = false 
    controller.stickerBrowserView.backgroundColor = UIColor.blue 
    controller.stickerBrowserView.dataSource = self 

    view.topAnchor.constraint(equalTo: controller.view.topAnchor).isActive = true 
    view.bottomAnchor.constraint(equalTo: controller.view.bottomAnchor).isActive = true 
    view.leftAnchor.constraint(equalTo: controller.view.leftAnchor).isActive = true 
    view.rightAnchor.constraint(equalTo: controller.view.rightAnchor).isActive = true 
    view.centerXAnchor.constraint(equalTo: controller.view.centerXAnchor).isActive = true 
    view.centerYAnchor.constraint(equalTo: controller.view.centerYAnchor).isActive = true 
} 

截图:https://d17oy1vhnax1f7.cloudfront.net/items/1F2B0s3v0s1k3E2L0Z07/Screen%20Shot%202016-09-19%20at%2011.42.51%20AM.png

+0

这可能是一个愚蠢的问题/我可能无法正确读取你的代码,但是有你错误地解决了你的限制?您是否应该根据旧视图的约束条件来设置新视图的约束条件?它看起来好像你是基于新的子控制器的视图为父控制器的视图设置约束。 – Sparky

+0

@Sparky你的意思是这样吗? controller.view.topAnchor.constraint(equalTo:view.topAnchor).isActive = true。我这样做,行为是一样的。 –

+0

@Sparky?对不起,我对iOS开发比较陌生。律'的帮助?谢谢! –

回答

0

更好地解释事情下面我已经把。这演示了修复子视图布局的两种方法。在使用约束时,我更愿意将约束作为数组创建,并一次性激活它们,正如您将在createredSquareWithConstraints的代码中看到的一样。一个约束只是一个线性方程,将一个视图的特征与另一个视图的特征相关联。例如,在“伪代码”中,我的数组中的第一个约束可写为:

“将子视图的前导边距设置为等于容器视图的前导边距的1倍加上常数0。

(这就是我为什么,因为它看着我,仿佛你是基于它的子视图的一个特点设置包含视图的约束是越来越早混淆。)

虽然仍完全有效利用布局约束条件,我认为现在的首选方法是重写viewWillTransitionToSize()委托方法,它只是简单地要求您在给定包含视图的大小的情况下指定视图控制器子视图的框架应该是什么。因此,我也包含了一个实现,创建一个初始帧的黄色方块,然后在调用viewWillTransitionToSize时进行修改。我个人觉得使用布局约束要少得多。

如果你随着按钮放置并旋转屏幕,你会发现任何一种方法都能达到同样的效果。 [注:我已经将一个方格标记为受限制,而另一个标记为不受约束,但实际上它们当然都受到约束,只是以不同的方式。我会补充一点,这显然不是你在实践中将如何做的事情 - 你应该选择一种方法并坚持下去,否则你的代码将会遍布全球!]。

希望有帮助!

import UIKit 

class ViewController: UIViewController { 

    var constrainedredSquare : UIView! 
    var unconstrainedRedSquare : UIView! 
    var methodOneButton : UIButton! 
    var methodTwoButton : UIButton! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     self.view.backgroundColor = UIColor.blue 

     func getButton(name: String) -> UIButton { 
      let button : UIButton = UIButton() 
      button.backgroundColor = UIColor.white 
      button.layer.cornerRadius = 3 
      button.clipsToBounds = true 
      button.setTitle(name, for: UIControlState.normal) 
      button.setTitleColor(UIColor.black, for: UIControlState.normal) 
      return button 
     } 

     self.methodOneButton = getButton(name: "Red - Constraints") 
     self.methodTwoButton = getButton(name: "Yellow - viewWillTransitionToSize") 

     self.methodOneButton.addTarget(self, action: #selector(self.createRedSquareWithConstraints), for: .touchUpInside) 
     self.methodTwoButton.addTarget(self, action: #selector(self.createYellowSquareWithoutConstraints), for: .touchUpInside) 
     self.methodOneButton.frame = CGRect(origin: CGPoint(x: 200, y: 100), size: CGSize(width: 300, height: 300)) 
     self.methodTwoButton.frame = CGRect(origin: CGPoint(x: self.view.frame.width - 500, y: 100), size: CGSize(width: 300, height: 300)) 
     self.view.addSubview(self.methodOneButton) 
     self.view.addSubview(self.methodTwoButton) 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { 
     if let _ = self.unconstrainedRedSquare { 
      self.unconstrainedRedSquare.frame = CGRect(origin: CGPoint.zero, size: size) 
     } 
     self.methodOneButton.frame = CGRect(origin: CGPoint(x: 200, y: 100), size: CGSize(width: 300, height: 300)) 
     self.methodTwoButton.frame = CGRect(origin: CGPoint(x: size.width - 500, y: 100), size: CGSize(width: 300, height: 300)) 
    } 


    func createYellowSquareWithoutConstraints() { 
     if let _ = self.unconstrainedRedSquare { 
      self.unconstrainedRedSquare.removeFromSuperview() 
     } 
     else 
     { 
      if let _ = constrainedredSquare { 
       self.constrainedredSquare.removeFromSuperview() 
      } 
      self.unconstrainedRedSquare = UIView() 
      self.unconstrainedRedSquare.backgroundColor = UIColor.yellow 
      self.unconstrainedRedSquare.frame = CGRect(origin: CGPoint.zero, size: self.view.frame.size) 
      self.view.addSubview(self.unconstrainedRedSquare) 

      self.view.bringSubview(toFront: self.methodOneButton) 
      self.view.bringSubview(toFront: self.methodTwoButton) 
     } 

    } 

    func createRedSquareWithConstraints() { 
     if let _ = self.constrainedredSquare { 
      self.constrainedredSquare.removeFromSuperview() 
     } 
     else 
     { 
      if let _ = self.unconstrainedRedSquare { 
       self.unconstrainedRedSquare.removeFromSuperview() 
      } 
      let redSquare : UIView = UIView() 
      redSquare.backgroundColor = UIColor.red 

      self.view.addSubview(redSquare) 

      self.view.bringSubview(toFront: self.methodOneButton) 
      self.view.bringSubview(toFront: self.methodTwoButton) 

      let rsConstraints : [NSLayoutConstraint] = [NSLayoutConstraint(item: redSquare, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.leading, multiplier: 1.0, constant: 0), 
               NSLayoutConstraint(item: redSquare, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.trailing, multiplier: 1.0, constant: 0), 
               NSLayoutConstraint(item: redSquare, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.top, multiplier: 1.0, constant: 0), 
               NSLayoutConstraint(item: redSquare, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 0), 
               NSLayoutConstraint(item: redSquare, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.width, multiplier: 1.0, constant: 0), 
               NSLayoutConstraint(item: redSquare, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.height, multiplier: 1.0, constant: 0)] 
      redSquare.translatesAutoresizingMaskIntoConstraints = false 
      NSLayoutConstraint.activate(rsConstraints) 
     } 
    } 
} 
0

您可以使用我的扩展名为UIView。它允许添加额外填充任何端(仅如果你想):

public extension UIView { 
    typealias ConstraintsTupleStretched = (top:NSLayoutConstraint, bottom:NSLayoutConstraint, leading:NSLayoutConstraint, trailing:NSLayoutConstraint) 
    func addSubviewStretched(subview:UIView?, insets: UIEdgeInsets = UIEdgeInsets()) -> ConstraintsTupleStretched? { 
     guard let subview = subview else { 
      return nil 
     } 

     subview.translatesAutoresizingMaskIntoConstraints = false 
     addSubview(subview) 

     let constraintLeading = NSLayoutConstraint(item: subview, 
                attribute: .Left, 
                relatedBy: .Equal, 
                toItem: self, 
                attribute: .Left, 
                multiplier: 1, 
                constant: insets.left) 
     addConstraint(constraintLeading) 

     let constraintTrailing = NSLayoutConstraint(item: self, 
                attribute: .Right, 
                relatedBy: .Equal, 
                toItem: subview, 
                attribute: .Right, 
                multiplier: 1, 
                constant: insets.right) 
     addConstraint(constraintTrailing) 

     let constraintTop = NSLayoutConstraint(item: subview, 
               attribute: .Top, 
               relatedBy: .Equal, 
               toItem: self, 
               attribute: .Top, 
               multiplier: 1, 
               constant: insets.top) 
     addConstraint(constraintTop) 

     let constraintBottom = NSLayoutConstraint(item: self, 
                attribute: .Bottom, 
                relatedBy: .Equal, 
                toItem: subview, 
                attribute: .Bottom, 
                multiplier: 1, 
                constant: insets.bottom) 
     addConstraint(constraintBottom) 
     return (constraintTop, constraintBottom, constraintLeading, constraintTrailing) 
    } 

} 

用法:

view.addSubviewStretched(tableView) 

let BorderedBackgroundInset = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1) 
view?.addSubviewStretched(calendar.view, insets: BorderedBackgroundInset) 
+0

不得不调整约束中的代码以符合最新版本的swift,但这个工作完美无缺,谢谢! –

+0

获取此消息“调用'addSubvieStretched(subview:insets :)'的结果未被使用。那是什么意思? –

+0

下面是使用上述方法生成的错误的更好屏幕截图:https://www.dropbox.com/s/8gi67ulcjuldm4k/Screenshot%202016-10-05%2012.24.48.png?dl=0和https:/ /www.dropbox.com/s/t9ks7d02coycicb/Screenshot%202016-10-05%2012.26.34.png?dl=0 –

相关问题