使用自动布局,当我更改标签文本时,标签位置会重置 [英] With autolayout, label position is reset when I change label text

查看:98
本文介绍了使用自动布局,当我更改标签文本时,标签位置会重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Objective-c开发一个非常简单的应用程序.
在该应用中,用户可以通过拖动屏幕来更改标签的位置.

I am developing a very simple application in Objective-c.
In the app, the user can change a position of the label by dragging the screen.

点击&拖动屏幕,标签将上下移动以匹配手指的位置.
松开手指时,标签的坐标信息将设置为其文本.

Tap & drag the screen, the label moves up and down to match the position of the finger.
When you release your finger, the coordinates information of the label is set to its text.

但是标签位置会在其文本更改时重置.

But the label position is reset at the moment its text is changed.

// IBOutlet UILabel *label

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touch = [[touches anyObject] locationInView:self.view];
    label.center = CGPointMake(label.center.x, touch.y);
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touch = [[touches anyObject] locationInView:self.view];
    label.center = CGPointMake(label.center.x, touch.y);
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touch = [[touches anyObject] locationInView:self.view];
    label.text = [NSString stringWithFormat:@"y = %f", touch.y];
}

touchesEnded 事件中,只需更改标签的文本,
但它的位置已重置.

In touchesEnded event, just only change the text of the label,
but the position of it has reset.

我试图像下面那样更改 touchesEnded 事件,但是它没有解决问题.

I tried to change touchesEnded event like following but it haven't solved the problem.

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touch = [[touches anyObject] locationInView:self.view];
    label.text = [NSString stringWithFormat:@"y = %f", touch.y];
    label.center = CGPointMake(label.center.x, touch.y);    // add this line
}

我想解决这种奇怪的行为,而无需取消选中使用自动布局" .
我想继续使用自动版式.

I want to resolve this weird behavior without uncheck "Use auto layout".
I'd like to keep using auto layout.

我的应用程序有4个屏幕截图.

I have 4 screenshots for my app.

4个屏幕截图

  1. 第一张图片是情节提要.
    我有一个带有自动布局约束的标签.
  2. 第二张图片是启动应用程序后的屏幕截图.
  3. 第三张图片是用户拖动屏幕时的图片,
    然后标签向下移动以匹配手指.
  4. 松开手指后的第四张图片
    标签文本已更改.
  1. The first image is a Storyboard.
    I have a label with auto layout constraints.
  2. The second image is a screenshot right after launching app.
  3. The third image is when a user drag the screen,
    and the label move down to match the finger.
  4. The forth image is right after release your finger
    and the label text has changed.

推荐答案

您不能使用自动布局更改事物的中心/框架;这些是相反的.做一个或另一个-不能两者都做.

You cannot use auto layout and change the center / frame of things; those are opposites. Do one or the other - not both.

因此,您不必关闭自动版式,但如果不这样做,则必须使用自动版式和仅自动版式定位事物.

So, you don't have to turn off auto layout, but if you don't, then you must use auto layout, and auto layout only, to position things.

移动标签时,请勿更改其中心-更改其约束.或者至少,改变了中心,改变了约束以匹配.

When you move the label, do not change its center - change its constraints. Or at least, having changed its center, change its constraints to match.

否则,当发生布局时,约束会将其放回您告诉他们放置的位置.更改文本时,布局确实发生.

Otherwise, when layout occurs, the constraints will put it back where you have told them to put it. And layout does occur when you change the text, and at many other times.

这篇关于使用自动布局,当我更改标签文本时,标签位置会重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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