You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自动布局约束报错:无法将UIButton转为NSLayoutAnchor,求解

布局约束类型不匹配问题原因分析

问题出在第二行约束代码:

retweetButton.centerYAnchor.constraint(equalTo: replyButton)

constraint(equalTo:)方法要求传入的参数是NSLayoutAnchor类型,但你直接传了replyButton这个UIButton实例,类型不匹配,所以触发了警告。

正确的写法应该是获取replyButton对应的垂直居中锚点,也就是replyButton.centerYAnchor,修正后代码如下:

retweetButton.leadingAnchor.constraint(equalTo: replyButton.trailingAnchor, constant: actionSpacing),
retweetButton.centerYAnchor.constraint(equalTo: replyButton.centerYAnchor), 

内容的提问来源于stack exchange,提问作者Alisher Saideshov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 12:05:30