以编程方式更改高度约束

2023-09-10移动开发问题
26

本文介绍了以编程方式更改高度约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想以编程方式更改 UITextView 的高度约束,因此我将约束设置为控制器中的插座,如下所示:

I want to change the height constraint of a UITextView programmatically so I set the constraint as an outlet in my controller like this:

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *descriptionHeightConstraint;

要更改它,我执行以下操作:

To change it I do the following:

self.descriptionHeightConstraint.constant = self.description.contentSize.height;

如果我在 viewDidAppear 中执行此操作,但问题是我可以看到显示视图后高度如何变化,这对用户不是很友好,所以我尝试在 中执行此操作>viewWillAppear 但没有在那里工作,高度没有改变.更改约束后调用 setNeedsUpdateConstraints 也不起作用.

This works if I do it in viewDidAppear but the problem with this is that I can see how the height changes after the view displayed which is not very user friendly so I tried doing it in viewWillAppear but didn't work there, the height doesn't change. Calling setNeedsUpdateConstraints after changing the constraint didn't work either.

为什么在 viewDidAppear 中工作而不在 viewWillAppear 中工作?有什么解决方法吗?

Why is working in viewDidAppear and not in viewWillAppear? Any workaround?

提前致谢!

推荐答案

尝试在 viewDidLayoutSubviews 中设置常量.

Try setting the constant in viewDidLayoutSubviews instead.

请注意,使用文本视图的 contentSize 属性设置文本视图的高度在 iOS 7 中不起作用.请参阅 https://stackoverflow.com/a/18837714/1239263

Note that using the text view's contentSize property to set the text view's height does not work in iOS 7. See https://stackoverflow.com/a/18837714/1239263

这篇关于以编程方式更改高度约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

[ios.cocos2d+box2d]如何禁用自动旋转?
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)...
2024-08-12 移动开发问题
7

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9

如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?
How can I implement a virtual joystick for a cocos2d game outside the cocos2d environment?(如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?)...
2024-08-12 移动开发问题
13