【问题标题】:search bar and table views are not fitting to the screen in land scape mode在横向模式下,搜索栏和表格视图不适合屏幕
【发布时间】:2023-10-30 04:48:01
【问题描述】:

我正在使用以下代码以编程方式创建搜索栏和表格视图

UISearchBar* searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0,320,40)];
searchBar.delegate = self;
[self.view addSubview:searchBar];

UITableView* tblView = [[UITableView alloc]initWithFrame:CGRectMake(0, 41, 320, 400)];
tblView.delegate = self;
tblView.dataSource = self;
[self.view addSubview:tblView];

现在我在以下方法中设置了返回值 yes 来旋转屏幕 wrt 设备方向

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

    return YES;
}

我在loadView中使用了以下代码

self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

以下是我的输出

但是在横向模式下,搜索栏和表格视图仍然不适合屏幕,我应该怎么做任何人都可以帮助我,谢谢提前

【问题讨论】:

标签: iphone objective-c xcode landscape uiinterfaceorientation


【解决方案1】:

您已经告诉视图调整大小,但您没有告诉您创建的搜索栏调整大小尝试类似:

searchbar.autorezingMask = UIViewAutoresizingFlexibleWidth;

对表格视图执行相同的操作。

看看这是否给你你正在寻找的结果。如果这不起作用,您始终可以在旋转后调整搜索栏和表格视图的框架。

【讨论】:

    【解决方案2】:

    尝试在 ViewWillAppear 方法中调用这个 autoresizingMask。

    【讨论】:

      【解决方案3】:

      如果您分配任何对象的框架...您必须在它旋转时提供新框架。 根据 toInterfaceOrientation 在下面的方法中给出一个新的框架。

      -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
      

      【讨论】: