• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 如何禁用单个 UI 元素的自动布局(UIlabel)

[复制链接]
菜鸟教程小白 发表于 2022-12-13 03:32:36 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我需要更改使用自动布局的 Viewcontroller 中标签的位置。在此之前,我使用了

[arriveTimeLabel setTranslatesAutoresizingMaskIntoConstraints:YES];
[departTimeLabel setTranslatesAutoresizingMaskIntoConstraints:YES];

但这会产生一些运行时警告

 Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2013-08-15 23:25:58.791 Vicinitime[78327:c07] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x9173a20 UIScrollView:0x9580190.bottom == UIView:0x9173890.bottom>",
    "<NSLayoutConstraint:0x9171fe0 V:|-(138)-[UILabel:0x958cf70]   (Names: '|':UIScrollView:0x9580190 )>",
    "<NSLayoutConstraint:0x91739a0 V:|-(0)-[UIScrollView:0x9580190]   (Names: '|':UIView:0x9173890 )>",
    "<NSAutoresizingMaskLayoutConstraint:0xab6ec90 h=--& v=&-& UILabel:0x958cf70.midY == -6.3989*UIScrollView:0x9580190.height>",
    "<NSAutoresizingMaskLayoutConstraint:0xab6eef0 h=--& v=&-& V:[UILabel:0x958cf70(25)]>",
    "<NSAutoresizingMaskLayoutConstraint:0xab6dbd0 h=--& v=--& V:[UIView:0x9173890(455)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x9171fe0 V:|-(138)-[UILabel:0x958cf70]   (Names: '|':UIScrollView:0x9580190 )>

所以我环顾四周,发现要解决我必须设置的问题 [arriveTimeLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; [departTimeLabel setTranslatesAutoresizingMaskIntoConstraints:NO];

没有。问题是现在标签没有以编程方式改变位置。

如果您需要编写代码以编程方式设置标签:

    if(travelTimeInt <= activityTimeInt){

        [timeBreakdownButtonImage setBackgroundImage:[UIImage imageNamed"timeBreakdownBackground7.png"] forState:UIControlStateNormal];
        NSLog(@"got to setting label place");

        [self.arriveTimeLabel  setFrame:CGRectMake(67, 170, 41, 25)];

        [self.departTimeLabel  setFrame:CGRectMake(211, 170, 41, 25)];



    }
    if(travelTimeInt * 7 >= activityTimeInt){

            [timeBreakdownButtonImage setBackgroundImage:[UIImage imageNamed"timeBreakdownBackground6.png"] forState:UIControlStateNormal];

          NSLog(@"got to setting label place");

            self.arriveTimeLabel.frame = CGRectMake(71, 170, 41, 25);

       self.departTimeLabel.frame = CGRectMake(207, 170, 41, 25);

    }

那么有没有办法在开启自动布局的情况下改变 uilabel 的位置?也许禁用自动布局?



Best Answer-推荐答案


如果您使用的是自动布局,那么您的 UIViews 将根据它们的 NSLayoutConstraints 进行定位。

要移动它们,而不是设置它们的 frames,您需要更改约束。

关于ios - 如何禁用单个 UI 元素的自动布局(UIlabel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18267111/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap