mercredi 22 juin 2016

Navigation bar in UIPageViewController bug


I have vertical UIPageViewController with scroll transition mode.
Bottom view controller have navigation controller without showing top bar. Top view controller have another navigation controller, but with showing top bar.

When I scroll to top VC navigation bar changes. Before ending of scroll animation frame correct. But after he suddenly change frame. I don't know how fixed this thing.

Before animation ending After animation ending

Any ideas?

PageViewController:

@interface FRTVerticalPageViewController () <UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate>
@property(strong, nonatomic) NSArray *controllers;

@end

@implementation FRTVerticalPageViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  self.dataSource = self;
  self.delegate = self;
  self.controllers = [self controllersToShowing];
  for (UIScrollView *view in self.view.subviews) {
    if ([view isKindOfClass:[UIScrollView class]]) {
      view.delegate = self;
      view.directionalLockEnabled = YES;
    }
  }

  [self setViewControllers:@[self.controllers[1]]
                 direction:UIPageViewControllerNavigationDirectionForward
                  animated:NO
                completion:nil];
}


#pragma mark - Public

- (void)showViewControllerForIndex:(NSInteger)index {
  UIViewController *currentVC = self.viewControllers.firstObject;
  if (currentVC != self.controllers[index]) {
    [self setViewControllers:@[self.controllers[index]]
                   direction:NO
                    animated:NO
                  completion:nil];
  }
}

#pragma mark - Private

- (NSArray<UIViewController *> *)controllersToShowing {
  UIViewController *mainPager = [self.storyboard instantiateViewControllerWithIdentifier:@"MainPageController"];

  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:kFRTProfileStoryboardName bundle:nil];
  UIViewController *profilePager = [storyboard instantiateViewControllerWithIdentifier:@"ProfilePageController"];

  NSArray *viewController = @[profilePager, mainPager];
  return viewController;
}

#pragma mark - UIPageViewControllerDataSource

- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
  NSInteger indexOfController = [self.controllers indexOfObject:viewController];
  if (indexOfController == 0) {
    return nil;
  }

  return self.controllers[indexOfController - 1];
}


- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
  NSInteger indexOfController = [self.controllers indexOfObject:viewController];
  if (indexOfController == self.controllers.count - 1) {
    return nil;
  }

  return self.controllers[indexOfController + 1];
}

#pragma mark - UIPageViewControllerDelegate

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers {
  self.inScrolling = YES;
  UIPageViewController *pageVC = self.viewControllers.firstObject;
  UINavigationController *navVC = pageVC.viewControllers.firstObject;
  NSLog(@"1:%@", NSStringFromCGRect(navVC.navigationBar.bounds));
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
  self.inScrolling = NO;
  UIPageViewController *pageVC = self.viewControllers.firstObject;
  UINavigationController *navVC = pageVC.viewControllers.firstObject; 
  NSLog(@"2:%@", NSStringFromCGRect(navVC.navigationBar.bounds));
}

Root View Controller in UINavigation Controller in Top of UIPageViewController:

@interface FRTProfileViewController () <UIImagePickerControllerDelegate,
                                        UINavigationControllerDelegate,
                                        UIActionSheetDelegate,
                                        TOCropViewControllerDelegate,
                                        LGAlertViewDelegate,
                                        UIAlertViewDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *userAvatarImageView;
@property (weak, nonatomic) IBOutlet UILabel *descriptionLabel;
@property (weak, nonatomic) IBOutlet UIButton *avatarSettingsButton;

@property (weak, nonatomic) IBOutlet UILabel *inboxCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *friendsCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *sentCountLabel;

@end

@implementation FRTProfileViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  self.avatarSettingsButton.hidden = YES;
  self.avatarSettingsButton.layer.masksToBounds = YES;
  self.avatarSettingsButton.layer.borderColor = [UIColor whiteColor].CGColor;
  self.avatarSettingsButton.layer.borderWidth = 2.f;
  __weak FRTProfileViewController *weakSelf = self;
  FRTUser *user = [FRTUserManager sharedManager].user;

  self.userAvatarImageView.layer.cornerRadius =
      CGRectGetWidth(weakSelf.userAvatarImageView.bounds) / 2;
  self.userAvatarImageView.layer.masksToBounds = YES;
  self.userAvatarImageView.layer.borderColor = [UIColor whiteColor].CGColor;
  self.userAvatarImageView.layer.borderWidth = 2.f;
  [self.userAvatarImageView sd_setImageWithURL:user.avatarImageURL
      placeholderImage:[UIImage imageNamed:@"im_avatar_placeholder"]
      completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
          weakSelf.avatarSettingsButton.hidden = NO;
      }];

  self.inboxCountLabel.text =
      self.friendsCountLabel.text =
      self.sentCountLabel.text = @"0";

}

- (void)viewDidLayoutSubviews {
  [super viewDidLayoutSubviews];

  [self.view layoutIfNeeded];
  self.userAvatarImageView.layer.cornerRadius =
    self.avatarSettingsButton.layer.cornerRadius =
      CGRectGetWidth(self.userAvatarImageView.bounds) / 2;
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];

  [self updateUserInfoInController];

  __weak FRTProfileViewController *weakSelf = self;
  [[FRTNetworkManager sharedManager] loadUserDetailsWithId:nil
      success:^(id responsedObject) {
          FRTUser *user = [MTLJSONAdapter modelOfClass:[FRTUser class]
                                    fromJSONDictionary:responsedObject
                                                 error:nil];
          [FRTUserManager sharedManager].user = user;
          [weakSelf updateUserInfoInController];
      } failure:^(NSError *error) {
          NSLog(@"User info loading failed. Reason:n%@", error.localizedDescription);
      }];
}

Aucun commentaire:

Enregistrer un commentaire