ios7 導航控制器切換影響UIScrollView布局的問題,ios7uiscrollview

來源:互聯網
上載者:User

ios7 導航控制器切換影響UIScrollView布局的問題,ios7uiscrollview

在 iOS 7 中,如果某個 UIViewController 的 self.view 第一個子視圖是 UIScollView, 同時當這個 UIViewController 被 push 或 initWithRootController 成為 UINavigationController控制的Controller時,這個 UIViewController的 view 的子視圖 UIScollView 的所有子視圖, 都會被下移 64px。   這個下移 64px 的前提是 navigationBar 和 statusBar 沒有隱藏。因為為 statusBar 預設的 Height 是 20px,而 navigatiBar  預設的 Height 是 44px。下面來比較一下執行個體:   不使用導航的介面跳轉    1. 在 AppDelegate.m 檔案中:Obj-c代碼 收藏代碼- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  {   self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];   self.window.backgroundColor = [UIColor whiteColor];                                 //下面兩行為增加的代碼                                                           ViewController *rootViewController = [[ViewController alloc] init];   [self.window setRootViewController:rootViewController];   [self.window makeKeyAndVisible];     return YES;  }  2. 在 ViewController.m 中:Obj-c代碼 收藏代碼- (void)viewDidLoad  {  [super viewDidLoad];  UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30.0,                                                           64.0, 260.0, 300.0)];  [scrollView setBackgroundColor:[UIColor redColor]];  UIView *view = [[UIView alloc] initWithFrame:scrollView.bounds];  [view setBackgroundColor:[UIColor blueColor]];  [scrollView addSubview:view];  [self.view addSubview:scrollView];  }  3. 運行後的結果:wKioL1M02bjAygEmAABp-pZVp3I608.jpg這種情況下,scrollView並未受影響。4. 現在使用 UINavigationController,  將開始 AppDelegate.m 增加的那兩行代碼修改成:  Obj-c代碼 收藏代碼ViewController *rootViewController = [[ViewController alloc] init];   UINavigationController *navController = [[UINavigationController alloc]                                  initWithRootViewController:rootViewController];   [self.window setRootViewController:navController];  5. 現在再次運行程式:wKiom1M02uuD8SEiAAB48M0Pmhc504.jpg如結果顯示, scrollView 背景色為藍色的子視圖位置自動下移了。 而這個下移的距離剛好是 64.0px。解決方案:   第一種:在 ViewController 的 init 的方法中增加一行代碼:Obj-c代碼 收藏代碼self.automaticallyAdjustsScrollViewInsets = NO;  第二種: 讓UIScrollView 不要成為 ViewController 的 View 的第一個子視圖。具體操作:將 viewDidLoad方法 修改成如下:Obj-c代碼 收藏代碼- (void)viewDidLoad  {  [super viewDidLoad];  UIView *firstSubView = [[UIView alloc] initWithFrame:self.view.bounds];  [self.view addSubview:firstSubView];  UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30.0,                                                           64.0, 260.0, 300.0)];  [scrollView setBackgroundColor:[UIColor redColor]];  UIView *view = [[UIView alloc] initWithFrame:scrollView.bounds];  [view setBackgroundColor:[UIColor blueColor]];  [scrollView addSubview:view];  [self.view addSubview:scrollView];  }  第三種:將 UIScorllView 的子視圖上移 64.0px 。修改 viewDidLoad 方法:Obj-c代碼 收藏代碼UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30.0,                                                           64.0, 260.0, 300.0)];  [scrollView setBackgroundColor:[UIColor redColor]];  CGRect viewFrame = CGRectMake(0, -64.0, CGRectGetWidth(scrollView.frame),                                                                                    CGRectGetHeight(scrollView.frame));  UIView *view = [[UIView alloc] initWithFrame: viewFrame];  [view setBackgroundColor:[UIColor blueColor]];  [scrollView addSubview:view];  [self.view addSubview:scrollView];  第四種:設定導覽列的透明屬性。self.navigationController.navigationBar.translucent = YES改變導覽列透明度,也會影響,這個可以根據自己的實際需求進行調整。


系統升級到iOS7,UIScrollView控制項滑動指示條消失,問怎解決?

我在uiscrollview上面add圖片,明明是CGRectMake(320*i, 0, 320, 160);但是顯示的時候卻是往下了幾十個像素,不解啊?另吐槽一下,程式一跑,各種控制項亂掉。。。 查看原帖>>
 
IOS問題:UIScrollView中加入UIViewController,也就是類似網易新聞首頁面的滑動效果

首先你要有個總控制器,管理UIScrollView的事件。將你想要添加的子頁面,也就是子Controller的View添加到你這個scollView上面就可以了。代碼就是[scrollView addSubview: subController.view]
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.