Objective c 自动调整面具是如何工作的?
我已经找到了很多关于自动调整大小的信息,但是没有一个能够解决我的问题。我有一个ViewController(RAViewController),它在loadView方法中调用视图,如下所示:Objective c 自动调整面具是如何工作的?,objective-c,ios,cocoa-touch,autoresizingmask,Objective C,Ios,Cocoa Touch,Autoresizingmask,我已经找到了很多关于自动调整大小的信息,但是没有一个能够解决我的问题。我有一个ViewController(RAViewController),它在loadView方法中调用视图,如下所示: - (void)loadView { // Set Navigation Bar Title self.navigationItem.title = @"RA"; // Add Background view self.view = [[UIView alloc] init
- (void)loadView
{
// Set Navigation Bar Title
self.navigationItem.title = @"RA";
// Add Background view
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Add RAView
RAView *rAView = [[RAView alloc] initWithFrame:self.view.frame Controller:self];
[self.view rAView];
}
它调用的视图(RAView)如下所示:
- (id)initWithFrame:(CGRect)frame Controller:(RAViewController *)Controller
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.autoresizesSubviews = YES;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.backgroundColor = [[UIColor alloc] initWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
controller = Controller;
// Draw Architecture View
[self drawArchitectureView];
}
return self;
}
-(void)drawArchitectureView {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, self.frame.size.width - 20, 50);
[button setBackgroundColor:[UIColor grayColor]];
[button setTitle:@"Overview" forState:UIControlStateNormal];
[self addSubview:button];
}
由于某些原因,当设备处于横向时,我无法使用自动调整大小掩码来调整按钮宽度。非常感谢您的帮助。如果您希望它保持相同的高度,但只需保持完全居中,从左到右10.0,请尝试:
-(void)drawArchitectureView {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, self.frame.size.width - 20, 50);
button.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
[button setBackgroundColor:[UIColor grayColor]];
[button setTitle:@"Overview" forState:UIControlStateNormal];
[self addSubview:button];
}
我相信你需要在UIButton上设置自动重设屏幕。试过了。按钮最终被从屏幕上移开,这不是我所期望的,这意味着我不太理解它是如何工作的。这也不是self.autoresizesSubviews=YES的要点代码>所以我不必这样做?您尝试将按钮autoresizeMask设置为什么?您是否尝试过UIViewAutoResizingFlexibleLeLeftMargin | UIViewAutoresizingFlexibleRightMargin?此外,autoresizesSubviews属性默认为YES,因此无需像您一样进行设置。这允许子视图的autoresizingMask属性工作。如果不是,就不会调整大小。好吧,因为我把它拿出来了。是的,它使按钮居中,但没有我想要的那么长。由于某些原因,高度和宽度值没有交换。