OGeek|极客世界-中国程序员成长平台

标题: ios - 如何确保我的 uiview 始终是正方形? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:21
标题: ios - 如何确保我的 uiview 始终是正方形?

我正在编写一个 uiview 子类,它必须显示在方形框架中才能正确格式化。

我如何编写 api 以便消费者只能请求一个正方形,并且它的框架的任何更改都会按比例发生?

或者在文档中让消费者知道该 View 仅适用于方形框架就足够了吗?



Best Answer-推荐答案


一种选择是覆盖自定义 View 类中的 setFrame: 方法:

- (void)setFrameCGRect)frame {
    if (frame.size.width != frame.size.height) {
         // Update this logic to suit your needs
         frame.size.height = frame.size.width;
    }

    [super setFrame:frame];
}

关于ios - 如何确保我的 uiview 始终是正方形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18027482/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4