关于edgesForExtendedLayout和automaticallyAdjustsScrollViewInsets

本文深入解析了在iOS应用中使用UIScrollView时,如何通过调整self.edgesForExtendedLayout与self.automaticallyAdjustsScrollViewInsets属性来优化与导航栏的交互,确保界面布局在不同iOS版本下的兼容性和一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UIScrollView来举例,在含有导航栏的页面内, self.edgesForExtendedLayout = UIRectEdgeNone; 调整的是UIScrollView本身的位置,self.automaticallyAdjustsScrollViewInsets = NO;调整的是UIScrollView显示内容的位置。

在iOS7之后self.edgesForExtendedLayout 默认值UIRectEdgeAll,此时坐标原点在屏幕左上角 ,在设置self.edgesForExtendedLayout = UIRectEdgeNone 会使坐标原点在导航栏左下角也就是说坐标原点回和iOS6一样,从这点来说设置self.edgesForExtendedLayout = UIRectEdgeNone达到的效果和设置

self.navigationController.navigationBar.translucent = NO效果一样。 self.edgesForExtendedLayout = UIRectEdgeNone是会对整个页面的控件的坐标起到作用的,而self.automaticallyAdjustsScrollViewInsets = NO 是只针对于UIScrollView的。

举个例子

//
//  AppDelegate.h
//  TES
//
//  Created by sjkx123456 on 15/4/2.
//  Copyright (c) 2015年 sjkx123456. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

//
//  AppDelegate.m
//  TES
//
//  Created by sjkx123456 on 15/4/2.
//  Copyright (c) 2015年 sjkx123456. All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    RootViewController * rootVC = [[RootViewController alloc] init];
    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:rootVC];
//    nav.navigationBar.translucent = NO;
//    nav.navigationBar.hidden = YES;
    self.window.rootViewController = nav;
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

//
//  RootViewController.h
//  TES
//
//  Created by sjkx123456 on 15/4/2.
//  Copyright (c) 2015年 sjkx123456. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
    UITableView * _goodsTabView;
}
@end

<pre name="code" class="objc">//
//  RootViewController.m
//  TES
//
//  Created by sjkx123456 on 15/4/2.
//  Copyright (c) 2015年 sjkx123456. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    
//    self.automaticallyAdjustsScrollViewInsets = NO;
    _goodsTabView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,320,240) style:UITableViewStylePlain];
    
    _goodsTabView.rowHeight = 44;
    _goodsTabView.dataSource = self;
    _goodsTabView.delegate = self;
    _goodsTabView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _goodsTabView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_goodsTabView];
    
//    UIView * v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
//    v.backgroundColor = [UIColor orangeColor];
//    [self.view addSubview:v];
}

#pragma mark UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID = @"cellID";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        cell.backgroundColor = [UIColor blueColor];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
    return cell;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end




如果self.edgesForExtendedLayout和self.automaticallyAdjustsScrollViewInsets都是默认值的情况下程序运行效果是这样的

如果self.edgesForExtendedLayout = UIRectEdgeNone;


 同时设置 self.edgesForExtendedLayout = UIRectEdgeNone;self.automaticallyAdjustsScrollViewInsets = NO;



只self.automaticallyAdjustsScrollViewInsetsNO;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值