0

我遇到了一个奇怪的问题。我正在使用 UISegmentedControl 在两个视图之间切换。当 UISegmentedViewController 出现在屏幕上时,会加载默认视图(view1)。View1 看起来与它应该的完全一样,但是当我单击 UISegmentedControl 更改视图时,view2 看起来不正确并被切断。当我现在切换回 view1 时,它也表现出相同的行为。我在下面附上了我用于此 UISegmentedViewController 的代码 - 我在 SO 帖子中找到了它。为了清楚起见,我将附上图片:

编辑:此外,课程部分中的最后一个表格视图单元格无法正确滚动到。为什么视野会被截断?

初始加载

点击教授部分后

回到最初的课程部分

代码:

//
//  PCFSegmentedRateViewController.m
//  Purdue Course Sniper
//
//  Created by Kamran Pirwani on 2/5/13.
//  Copyright (c) 2013 Kamran Pirwani. All rights reserved.
//

#import "PCFSegmentedRateViewController.h"

@interface PCFSegmentedRateViewController ()
// Array of view controllers to switch between
@property (nonatomic, copy) NSArray *allViewControllers;

// Currently selected view controller
@property (nonatomic, strong) UIViewController *currentViewController;

@end


@implementation PCFSegmentedRateViewController
@synthesize classRating,professorRating,segmentedControl;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [segmentedControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
    classRating = [[self storyboard] instantiateViewControllerWithIdentifier:@"ClassRate"];
    professorRating = [[self storyboard] instantiateViewControllerWithIdentifier:@"Professor"];
    // Add A and B view controllers to the array
    self.allViewControllers = [[NSArray alloc] initWithObjects:classRating, professorRating, nil];
    [self cycleFromViewController:self.currentViewController toViewController:[self.allViewControllers objectAtIndex:self.segmentedControl.selectedSegmentIndex]];
}

#pragma mark - View controller switching and saving

- (void)cycleFromViewController:(UIViewController*)oldVC toViewController:(UIViewController*)newVC {

    // Do nothing if we are attempting to swap to the same view controller
    if (newVC == oldVC) return;

    // Check the newVC is non-nil otherwise expect a crash: NSInvalidArgumentException
    if (newVC) {

        // Set the new view controller frame (in this case to be the size of the available screen bounds)
        // Calulate any other frame animations here (e.g. for the oldVC)
        newVC.view.frame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMinY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));

        // Check the oldVC is non-nil otherwise expect a crash: NSInvalidArgumentException
    if (oldVC) {

            // Start both the view controller transitions
            [oldVC willMoveToParentViewController:nil];
            [self addChildViewController:newVC];

            // Swap the view controllers
            // No frame animations in this code but these would go in the animations block
            [self transitionFromViewController:oldVC
                              toViewController:newVC
                                      duration:0.25
                                       options:UIViewAnimationOptionLayoutSubviews
                                    animations:^{}
                                    completion:^(BOOL finished) {
                                        // Finish both the view controller transitions
                                        [oldVC removeFromParentViewController];
                                        [newVC didMoveToParentViewController:self];
                                        // Store a reference to the current controller
                                        self.currentViewController = newVC;
                                    }];

        } else {

            // Otherwise we are adding a view controller for the first time
            // Start the view controller transition
            [self addChildViewController:newVC];

            // Add the new view controller view to the ciew hierarchy
            [self.view addSubview:newVC.view];

            // End the view controller transition
            [newVC didMoveToParentViewController:self];

            // Store a reference to the current controller
            self.currentViewController = newVC;
        }
    }
}

- (IBAction)indexDidChangeForSegmentedControl:(UISegmentedControl *)sender {

    NSUInteger index = sender.selectedSegmentIndex;

    if (UISegmentedControlNoSegment != index) {
        UIViewController *incomingViewController = [self.allViewControllers objectAtIndex:index];
        [self cycleFromViewController:self.currentViewController toViewController:incomingViewController];
    }

}
@end
4

0 回答 0