cuốn sách gpt4 ai đã làm

ios - 如何实现不同长宽比的不同 View 布局?

In lại Tác giả: Walker 123 更新时间:2023-11-29 05:46:49 25 4
mua khóa gpt4 Nike

我正在创建一个名为“canvasView”的 UIView 并向该 View 添加贴纸、文本和照片。假设如果应用程序更改了“canvasView”的布局或大小,那么如何调整所有 subview 的位置和大小。

请参阅下图了解更多详细信息。

nhập mô tả hình ảnh ở đây

câu trả lời hay nhất

正确的方法是将 subview 的创建及其大小和位置分开。例如,在 canvasView 类中,您可以在 init 函数之一中创建 subview 。然后,您在 layoutSubviews 中进行实际的大小调整和放置。每当您的 View 大小发生变化时(包括最初创建时),都会调用此函数。

- (void)layoutSubviews {
[super layoutSubviews];

// position and size each sub-view here
// self.bounds.size contains your new view size
// Your sub-views should already be created when coming here since this function is called multiple times

// You can get your sub-views either by storing them as properties in `canvasView` like this
self.oneoneButton.frame = CGRectMake(10.0, self.bounds.size.height / 2, 40.0, 40.0);

// or simply set a unique `tag` on each and find them by calling `[self viewWithTag:xxx]`
UIView *v = [self viewWithTag:101]; // 101 is the id for 1:1 button
v.frame = CGRectMake(10.0, self.bounds.size.height / 2, 40.0, 40.0);
}

关于ios - 如何实现不同长宽比的不同 View 布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56077386/

25 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress