objective-c – 使用autolayout,当我更改标签文本时,标签位置会
发布时间:2020-12-15 01:41:40 所属栏目:百科 来源:网络整理
导读:我正在Objective-c中开发一个非常简单的应用程序. 在应用程序中,用户可以通过拖动屏幕来更改标签的位置. Tap拖动屏幕,标签上下移动以匹配手指的位置. 松开手指时,标签的坐标信息将设置为其文本. 但标签位置在文本更改时重置. // IBOutlet UILabel *label- (v
我正在Objective-c中开发一个非常简单的应用程序.
在应用程序中,用户可以通过拖动屏幕来更改标签的位置. Tap&拖动屏幕,标签上下移动以匹配手指的位置. 但标签位置在文本更改时重置. // 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事件中,只需更改标签的文本, 我试图改变touchesEnded事件如下,但它没有解决问题. - (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 } 我想在不取消选中“使用自动布局”的情况下解决这种奇怪的行为. 我的应用程序有4个截图. 4 Screenshots >第一张图片是故事板. 解决方法
你不能使用自动布局和改变事物的中心/框架;那些是对立的.做一个或另一个 – 不是两个.
因此,您不必关闭自动布局,但如果不这样做,则必须使用自动布局和自动布局来定位. 移动标签时,请勿更改其中心 – 更改其约束.或者至少,改变了它的中心,改变其约束以匹配. 否则,当布局发生时,约束会将它放回到您告诉它们放置它的位置.当您更改文本时,以及在许多其他时间,布局确实会发生. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |