gpt4 book ai didi

ios - 旋转时自定义 UIView 调整大小

转载 作者:行者123 更新时间:2023-11-28 22:27:51 25 4
gpt4 key购买 nike

我知道有几个类似的问题,但我仍然没有找到合适的解决方案。我刚刚创建了一个带有自定义 View 的测试项目:

@interface CustomView : UIView

//..

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor redColor]];

}
return self;
}

和 View Controller :

- (void)viewDidLoad
{
[super viewDidLoad];

CustomView *cv = [(CustomView *) [CustomView alloc] initWithFrame:self.view.frame];
[self.view addSubview: cv];
}

在横向模式下我们可以看到:

enter image description here

如何解决?我知道 layoutSubviews,但是 1. 旋转时不会调用它; 2. 如何在这里调整 View 的大小?

最佳答案

尝试设置 autoresizingMask。

CustomView *cv = [(CustomView *) [CustomView alloc] initWithFrame:self.view.frame];
cv.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview: cv];

你也可以看看自动布局

https://developer.apple.com/library/ios/recipes/xcode_help-interface_builder/articles/UnderstandingAutolayout.html

问候,

关于ios - 旋转时自定义 UIView 调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18417870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com