gpt4 book ai didi

ios - 景观 subview 未填充 self.view

转载 作者:行者123 更新时间:2023-11-29 04:06:12 27 4
gpt4 key购买 nike

我有一个 UIView,其 subview 在旋转到横向模式后不想完全填充 self.view。我尝试了很多方法,从设置自动调整大小蒙版到设置旋转后 subview 的框架,它们要么不起作用,要么问题变得更糟。下面是一个 UINavigationBar 遇到此问题的示例(导航栏没有完全填充横向宽度)。代码:

- (void)loadView {
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
self.view.backgroundColor = [UIColor blueColor];
myBar =[[UINavigationBar alloc] init];
myBar.frame = CGRectMake (0, 0, self.view.frame.size.width, 44);
UINavigationItem *navItem =[[[UINavigationItem alloc] initWithTitle:@"Title"] autorelease];
UIBarButtonItem *rightButton =[[[UIBarButtonItem alloc] initWithTitle: @"Email" style: UIBarButtonItemStylePlain target: self action:@selector (rightButtonPressed)] autorelease];
navItem.rightBarButtonItem = rightButton;
UIBarButtonItem *leftButton =[[[UIBarButtonItem alloc] initWithTitle: @"About" style: UIBarButtonItemStylePlain target: self action:@selector (leftButtonPressed)] autorelease];
navItem.leftBarButtonItem = leftButton;
myBar.barStyle = UIBarStyleDefault;
myBar.items =[NSArray arrayWithObject:navItem];
[self.view addSubview:myBar];
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
@end

最佳答案

只需在代码末尾添加一行代码:

- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor blueColor];
UINavigationBar *myBar;
myBar =[[UINavigationBar alloc] init];
myBar.frame = CGRectMake (0, 0, self.view.frame.size.width, 44);
UINavigationItem *navItem =[[UINavigationItem alloc] initWithTitle:@"Title"];
UIBarButtonItem *rightButton =[[UIBarButtonItem alloc] initWithTitle: @"Email" style: UIBarButtonItemStylePlain target: self action:@selector (rightButtonPressed)];
navItem.rightBarButtonItem = rightButton;
UIBarButtonItem *leftButton =[[UIBarButtonItem alloc] initWithTitle: @"About" style: UIBarButtonItemStylePlain target: self action:@selector (leftButtonPressed)];
navItem.leftBarButtonItem = leftButton;
myBar.barStyle = UIBarStyleDefault;
myBar.items =[NSArray arrayWithObject:navItem];
[self.view addSubview:myBar];
[myBar setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
}

我删除了 autorelease,因为我使用了 ARC。您可以再次添加自动释放。祝 friend 有美好的一天。

关于ios - 景观 subview 未填充 self.view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15170165/

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