如何用Swift以编程方式增加UIView的高度 [英] How to programmatically increase the height of UIView with Swift

查看:311
本文介绍了如何用Swift以编程方式增加UIView的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在IB内部,我有一个带有segmentedControl和2个containerViews的viewController。在每个containerView中我都有一个tableView。从每个tableView我推送一个detailView。我的自动布局在IB中。

Inside IB I have a viewController with a segmentedControl and 2 containerViews. In each containerView I have a tableView. From each tableView I push on a detailView. My auto layout is in IB.

一旦我选择一个片段,我会看到相应的tableView,我选择一个单元格,并在场景中推送正确的detailView。

Once I pick a segment, I see the corresponding tableView, I pick a cell and the correct detailView gets pushed on the scene.

问题是一旦在segmentedControl上推送detailView仍然可见。我决定隐藏有效的segmentedControl,但那里有一个很大的空白空间。我尝试以编程方式增加detailView视图的大小,但它没有扩展。从我读到的是因为我在故事板中做了初步限制。

The issue is once the detailView gets pushed on the segmentedControl is still visible. I decided to hide the segmentedControl which works but there is a big empty space there. I tried to programmatically increase the size of detailView's view but it's not expanding. From what I read it's because I made the initial constraints in storyboards.

如何以编程方式获取detailView的视图以进行扩展?

代码:

DetailViewController: UIViewController{

override func viewDidLoad() {
        super.viewDidLoad()

 //this isn't increasing the view's size
 self.view.frame.size.height += 20.0

 //doesn't work. I get an error on the last argument
 self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height += 20.0)

 //doesn't work. I get an error on the last argument
 self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.size.height += 20.0)
 }

}

错误:

//Error for the last argument- height: *self.view.frame.height += 20.0*
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height += 20.0)




变异运算符的左侧不可变:'height'是get-only
属性

Left side of mutating operator isn't mutable: 'height' is a get-only property



//Error for the last argument- *self.view.frame.size.height += 20.0*:
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.size.height += 20.0)




无法使用
类型的参数列表调用类型为'CGRect'的初始值设定项'(x:Int,y:Int,width:CGFloat,height :())'

Cannot invoke initializer for type 'CGRect' with an argument list of type '(x: Int, y: Int, width: CGFloat, height: ())'


推荐答案

您需要为详细视图的高度约束创建一个出口,然后您可以调整高度编程,如下所示 myHeightConstraint.constant + = extraHeight 其中 extraHeight 是一个数字,表示您想要详细视图的高度成为。之后您还可能需要调用 self.view.layoutIfNeeded()

You need to create an outlet for the height constraint of the detail view, and then you can adjust the height programmatically like this myHeightConstraint.constant += extraHeight where extraHeight is a number indicating how much taller you want the detail view to be. You also may need to call self.view.layoutIfNeeded() afterwards.

从约束创建出口,控制从故事板编辑器中的约束(就像你对UIView的出口一样)拖动到你的代码中。

To create an outlet from a constraint, control drag from the constraint in the storyboard editor (just like you would for an outlet of a UIView) into your code.

你是对的 - 因为你正在使用自动布局和约束,所以也需要使用约束进行调整。设置原始帧可能会导致意外行为。如果您有任何其他问题或困难,请告诉我。

You are right - because you are using auto layout and constraints, the adjustments need to be made with constraints too. Setting the raw frames can lead to unexpected behavior. Let me know if you have any other questions or difficulties.

这篇关于如何用Swift以编程方式增加UIView的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆