我使用以下代码更改UIView的位置,而不更改视图的大小.

CGRect f = aView.frame;
f.origin.x = 100; // new x
f.origin.y = 200; // new y
aView.frame = f;

是否有更简单的方法只更改视图位置?

推荐答案

aView.center = CGPointMake(150, 150); // set center

aView.frame = CGRectMake( 100, 200, aView.frame.size.width, aView.frame.size.height ); // set new position exactly

aView.frame = CGRectOffset( aView.frame, 10, 10 ); // offset by an amount

Edit:

I didn't compile this yet, but it should w或k:

#define CGRectSetPos( r, x, y ) CGRectMake( x, y, r.size.width, r.size.height )

aView.frame = CGRectSetPos( aView.frame, 100, 200 );

Ios相关问答推荐

设置托管在DigitalOcean上的iOS通用链接

如何在不将应用程序留在SwiftUI上的情况下更改语言?

clang:错误:SDK 路径中不包含libarclite

如何删除点击时按钮的不透明动画?

如何使用 pushViewController 从 UIHostingController 的 SwiftUI 视图中推送 ViewController?

如何在 Mac OS Ventura 中使用 Xcode 13

iOS SwiftUI - 使用 PhotosPicker 从图库中 Select 图像或视频

工具栏底部 iOS 16 - SwiftUI

小部件链接有效,但不执行任何操作

具有多个目标的应用程序的动态内容

无法为 Flutter 项目构建 ipa [CocoaPods 找不到 pod "GoogleAppMeasurement" 的兼容版本:]

如何让我的 iOS 应用程序与 api 连接?

禁用 UITextField 的自动更正

iOS 中的 [Class new] 和 [[Class alloc] init] 有什么区别?

Xcode 4 + iOS 4.3:存档类型不存在打包程序

如何删除 Swift 数组中的所有 nil 元素?

如何在 Android 和 iPhone 的移动应用程序中实施推荐计划

从 iOS/Android 上的 Web 应用调用本机日期 Select 器

如何找到 UILabel 的实际行数?

这是一个什么样的讽刺错误?