iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了objective-c – 从Landscape到Portrait(iPad)旋转后UITableView的奇怪行为大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在方法中旋转后读取tableview的contentOffset

willRotateToInterfaceOrientation:时间:.我使用UITableViewController和tableHeaderView中的搜索栏创建了一个示例.

场景1:
给定设备在纵向,我隐藏搜索
然后我将设备旋转到横向
之后,我希望不要看到UISearchbar和contentOffset保持不变.

场景2:
给定设备在横向,我隐藏搜索
然后我将设备旋转为肖像
之后,我希望不要看到UISearchbar和contentOffset保持不变.

方案1按预期工作.场景2弹出搜索栏,内容偏移为零

有人知道为什么ContentOffset是零吗?我希望它是44(搜索栏的高度).

有什么方法可以解决这个问题吗?你会怎么做?

//
//  ViewController.m
//  test
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (id)initWithStyle:(UITableViewStylE)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve SELEction between presentations.
    // self.cleaRSSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

}

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

    //is necessary to prevent showing searchbar
    dispatch_async(dispatch_get_main_queue(),^{


        double y = self.tableView.contentOffset.y;

        self.tableView.contenTinset = UIEdgeInsetsmake(-1*y,0);

        NSLog(@"y %f",y);
        NSLog(@"Begin offset %@",NSStringFromCGPoint(self.tableView.contentOffset));
    });

}

- (void)viewDidAppear:(BOOL)animated{

    self.tableView.tableHeaderView = [[UISearchBar alloc] initWithFrame:CGRectMake(0,self.tableView.frame.size.width,44)];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reusEIDentifier:CellIdentifier];
    }

    // Configure the cell...

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSELEctRowATindexPath:(NSIndexPath *)indexPath
{

}

@end

解决方法

我想我知道问题出在哪里:

UITableView具有认的自动调整遮罩.因此,在将设备从纵向旋转到横向之后,UITableView变得越来越小(不会改变偏移).如果用户现在回到纵向,则需要拉伸UITableView并自动滚动到顶部.为了解决这个问题,我使用了一个变量来注册每个“用户滚动”

> scrollViewWillBeginDragging(注册)
> scrollViewDidEndDecelerating(unregistratE)
>在“scrollViewDidScroll”中我检查滚动是否来自用户(如果来自用户保存偏移值)

最后在方法“willRotateToInterfaceOrientation”中,我将临时保存的偏移量设置为UITableView,以使其保持在同一位置.

我希望有更好的解决方案,因为我解决这个问题的方法有点棘手.

@H_197_47@

大佬总结

以上是大佬教程为你收集整理的objective-c – 从Landscape到Portrait(iPad)旋转后UITableView的奇怪行为全部内容,希望文章能够帮你解决objective-c – 从Landscape到Portrait(iPad)旋转后UITableView的奇怪行为所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。