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

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

问题描述

我已经使用界面"构建器在视图中插入了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 programmatically. 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的大小.在笔尖中设置插座,以使您的代码可以访问需要更改的约束.您可以实时设置约束的constant,通常就足够了.

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.

只是为了阐明原因:如果视图的框架受约束定位,则无法更改视图的框架.好吧,可以,但这是徒劳的,而且是不好的做法.这是因为布局系统将使用约束本身来为您更改视图的框架!因此,您可以更改框架,但是布局系统将读取并解决约束,然后再次更改框架.

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天全站免登陆