ios - 检测 View 已成功更新框架

标签 ios objective-c uibutton

我在 UIView 中有一个 UIButton 并且我在 (instancetype)initWithFrame 中以编程方式设置了 UIButton 的约束宽度:(CGRect)frame { 使用 PureLayout 类似

self.errorButtonConstraintWidth = [self.btnError autoSetDimension:ALDimensionWidth toSize:5];
self.errorButtonConstraintWidth.constant = 40;
NSLog(@"afterChangeContraintWith = %f",self.btnError.frame.size.width);

但是设置约束后,打印按钮框架的 Log 总是打印 0。我想在我改变框架约束后不会立即更新。
如何检测我的 View 何时已成功更新?

之所以要检测我的 View 何时成功更新,是因为我想在将按钮宽度更改为 40 后,在中心水平位置和此 UIButton 下方显示一个下拉列表

最佳答案

frame不会立即设置,会在下一次布局过程中设置。

但你可以这样做

方法一

override func layoutSubviews() {
    super.layoutSubviews()

    // in this method, the frame has been set.
}

方法二

// This will force autolayout to determine the frame
// without waiting for the next layouting schedule
self.layoutIfNeeded()

关于ios - 检测 View 已成功更新框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37878579/

相关文章:

ios - PhoneGap SVG 在 iOS 上作为 data-uri

ios - Objective-C 消息被发送到地址为 : 的已释放 'NSThread' 对象(僵尸)

ios - 在按钮边缘绘制框架

IOS 6 和 Auto Layout 自定义按钮高度错误

php - iOS 9.1 导致我的 JSON NSURL 连接到 PHP 页面时出现错误

添加 didReceiveRemoteNotification 时 iOS AppDelegate 崩溃

ios - GeoCoder 使用地址获取经度和纬度

ios - 使用 UiButton 作为切换开关时出现问题

ios - SpriteKit - 更改节点上的 anchor 会更改所有子节点的位置

ios - 安装 Xcode 9.0 Beta 后 Xcode 8.3.2 中缺少模拟器