Ios UI分段视图控制器切断视图
我遇到了一个奇怪的问题。我正在使用UISegmentedControl在两个视图之间切换。当UISegmentedViewController出现在屏幕上时,会加载默认视图(view1)。view1的外观与应有的完全一致,但当我单击UISegmentedControl以更改视图时,view2的外观不正确,因此被切断。当我现在切换回view1时,它也表现出相同的行为。我在下面附上我用于此UISegmentedViewController的代码-我在SO帖子中找到了它。为了清晰起见,我将附上图片: 编辑:此外,课程部分中的最后一个tableview单元格无法正确滚动到。为什么风景被切断了 代码:Ios UI分段视图控制器切断视图,ios,objective-c,cocoa-touch,uiview,uisegmentedcontrol,Ios,Objective C,Cocoa Touch,Uiview,Uisegmentedcontrol,我遇到了一个奇怪的问题。我正在使用UISegmentedControl在两个视图之间切换。当UISegmentedViewController出现在屏幕上时,会加载默认视图(view1)。view1的外观与应有的完全一致,但当我单击UISegmentedControl以更改视图时,view2的外观不正确,因此被切断。当我现在切换回view1时,它也表现出相同的行为。我在下面附上我用于此UISegmentedViewController的代码-我在SO帖子中找到了它。为了清晰起见,我将附上图片:
很高兴看到,所以我会接受这么大的图形尺寸。天哪,好妈妈。所以你需要在切换后记录视图的帧。您显示的视图可能是另一个视图的子视图,框架不匹配,或者弹簧和支柱(或自动布局)设置不正确。这让追踪变得乏味,但肯定是根本问题。我总是使用UIViewController基类和一些辅助调试方法,这些方法允许我在调用时转储子视图和超级视图-帧、调整大小掩码、isHidden等-这些在遇到像您这样的问题时非常方便。嘿,当我更改UITableView的弹簧和支柱时,效果很好。但最后一个细胞仍在被切断。有什么建议吗?
//
// 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