无法以编程方式更改UITextView框架大小 [英] Can't change UITextView frame size programatically

查看:80
本文介绍了无法以编程方式更改UITextView框架大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个带有Interface构建器的视图中插入了一个UITextView,现在我想改变它的帧大小,使它适合程序化的内容。问题是由于约束,大小似乎被锁定并且不能从代码中取消。如果我在文件检查器中禁用使用自动布局,则每个对象都会删除约束,但我只想更改UITextView而不是其他对象。

I've insert an UITextView in a view with Interface builder, now I would like to change its frame size so it fits the content progrmmatically. The problem is that the size seems locked and unchangable from code because of the constraints. If I disable in file inspector use auto-layout every object gets the constraints removed, but I only want to change the UITextView not the other objects.

[textview setFrame:CGRectMake(x,y,w,h)]; // This doesn't do anything to the uitextview


推荐答案

如果您正在使用约束,则必须使用约束来更改UITextView的大小。在笔尖中设置出口,以便从代码访问您需要更改的约束。您可以实时设置约束的常量,这通常就足够了。

If you're using constraints, then you must use constraints to change the UITextView's size. Set up outlets in the nib to get access from your code to the constraints you need to change. You can set a constraint's constant in real time, and this is usually sufficient.

只是为了澄清原因this:如果按约束定位视图框架,则无法更改视图框架。好吧,你可以,但它没有结果,这是不好的做法。这是因为布局系统将使用约束本身来为您更改视图的框架!因此,您可以更改框架,但布局系统将读取并解决约束并再次更改框架。

Just to clarify the reasons for this: you cannot change a view's frame if it is being positioned by constraints. Well, you can, but it's fruitless and it's bad practice. This is because the constraints themselves will be used by the layout system to change the view's frame for you! Thus, you can change the frame, but the layout system will then read and resolve the constraints and change the frame back again.

将约束视为待办事项列表用于布局系统。制约因素本身并无任何作用;它们只是一个规则列表。它是完成工作的布局系统(当调用 layoutSubviews 时)。每次需要布局时,布局系统都会出现,读取约束,找出如何遵守约束,并通过设置视图的框架来实现。您需要使用该系统,而不是反对它。

Think of constraints as a "to-do list" for the layout system. Constraints do nothing in and of themselves; they are just a list of rules. It is the layout system that does the work (when layoutSubviews is called). Every time layout is needed, the layout system comes along, reads the constraints, works out how to obey them, and does so - by setting the frames of your views. You need to work with that system, not against it.

这篇关于无法以编程方式更改UITextView框架大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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