UISegmentedViewController切断视图

时间:2013-05-07 21:36:02

标签: ios objective-c cocoa-touch uiview uisegmentedcontrol

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

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

Initial Loadup

After clicking on the Professors segment

Going back to the initial Courses segment

代码:

//
//  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

0 个答案:

没有答案
相关问题