必須有,你犯的錯誤代碼,我寫了一個演示: 我寫兩個控制器這樣的:
在vc1
:
#import "ViewController.h"
#import "ViewController2.h"
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initUI];
}
#pragma mark - init
- (void)initUI {
self.view.backgroundColor = [UIColor whiteColor];
A) If use storyboard, init vc2 like this :
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController2 *vc2 = [sb instantiateViewControllerWithIdentifier:@"ViewController2"];
B)If use code ,init vc2 like this:
ViewController2 *vc2 = [[ViewController2 alloc] init];
[self addChildViewController:vc2];
vc2.view.frame = self.view.bounds
[self.view addSubview:vc2.view];
vc2.view.alpha = 0;
[vc2 didMoveToParentViewController:self];
[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^
{
vc2.view.alpha = 1;
}
completion:nil];
}
@end
在vc2
:
#import "ViewController2.h"
@interface ViewController2()
@end
@implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor brownColor];
}
爲'viewController.view'嘗試'bringSubviewToFront' –
它不工作bro –
請確保childViewController(即。 viewController)與當前的視圖控制器不一樣,你不是將它添加到viewDidLoad或viewWillAppear等。 –