私は固まっています!私は、他のviewControllerのベースになるviewControllerに取り組んでいます。他はそれからも伸びるでしょう。UIViewは、自動レイアウトでプログラムで作成すると遅く表示されます
という名前のビュー(カスタムNavigationBarのような)をプログラムで作成したいのですが、baseViewControllerのを作成しました。
私はstoryboardを持っているので、rootViewController(HomeViewController)を持つnavigationControllerがあり、このhomeViewControllerはbaseViewControllerから拡張されています。
私のストーリーボードは次のようになります。
だから、私の制約が働いています!しかし、プログラムの制約で作成されたビューは、数秒後に表示され、そのドライブは私を狂ってしまう!
アプリの最初の実行時の表示方法は次のとおりです。
そして、数秒後に私のビューが表示されます。
私も自分のコードを共有したいです。私は制約を作成するためにビジュアルフォーマット言語を使用しています。
- (void)viewDidLoad {
[super viewDidLoad];
[self initializeBaseView];
// Do any additional setup after loading the view.}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Initialize BaseView
- (void)initializeBaseView{
//Background View
if (self.backgroundContainerView == nil) {
self.backgroundContainerView = [UIView new];
self.backgroundContainerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.backgroundContainerView];
[self.view sendSubviewToBack:self.backgroundContainerView];
}
[self initConstraint_1];
//Navigation Header View
if(self.navigationBarContainerView == nil){
self.navigationBarContainerView = [UIView new];
self.navigationBarContainerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.navigationBarContainerView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:self.navigationBarContainerView];
}
[self initConstraint_2];
if (self.backgroundType==BackgroundTypeWhite) {
//very light Gray
[self.backgroundContainerView setBackgroundColor:APP_GRAY_COLOR_ATHENS];
}
[self initializeBaseStyle];
}
- (void)initializeBaseStyle{
[self.navigationBarContainerView setBackgroundColor:[UIColor darkGrayColor]];
}
#pragma mark - Initialize Constraints
- (void)initConstraint_1{
// 1. Create a dictionary of views
NSDictionary *viewsDictionary = @{@"firstView":self.backgroundContainerView};
NSDictionary *metrics = @{@"vSpacing":@0, @"hSpacing":@0};
// 2. Define the view Position and automatically the Size
NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-vSpacing-[firstView]-hSpacing-|"
options:0
metrics:metrics
views:viewsDictionary];
NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hSpacing-[firstView]-hSpacing-|"
options:0
metrics:metrics
views:viewsDictionary];
[self.view addConstraints:constraint_POS_V];
[self.view addConstraints:constraint_POS_H];
}
- (void)initConstraint_2{
NSDictionary *viewsDictionary = @{@"firstView": self.navigationBarContainerView, @"secondView": self.logoImage};
NSDictionary *metrics = @{@"vSpacing":@74,
@"hSpacing":@0,
@"BottomSpacing":@60,
@"firstViewHeight":@64
};
// 2. Define the view Position and automatically the Size (for the firstView)
NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[firstView(firstViewHeight)]"
options:0
metrics:metrics
views:viewsDictionary];
NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hSpacing-[firstView]-hSpacing-|"
options:0
metrics:metrics
views:viewsDictionary];
[self.view addConstraints:constraint_POS_V];
[self.view addConstraints:constraint_POS_H];
}
私にはたくさんの質問があります。
- 理由は何でしょうか。
- プログラムで制約を作成したいときにストーリーボードを使用できますか?
- viewDidLoadを使用して、 ビューを作成し、制約を設定するメソッドを呼び出しています。 viewDidLoadはそれに適していますか?
- このシナリオのベストプラクティスは何ですか?
- どのように最初の画像のようにすぐに私のビューを初期化することができますか?
私はそれがはっきりしていることを望みます。 あなたの答えをありがとう、私はあなたからの聴聞を楽しみにしています。
viewDidAppearで試してみてください。 ViewDidloadはビューと自動レイアウトを処理するための適切な場所ではありません –
ビューを表示するのに2秒かかる理由はわかりません。私が言うことは、あなたのコードはあなたがやっていることに対して非常に複雑に見えるということです。 2つの提案:コードではなくストーリーボードに制約を設定します。これはずっと簡単です。次に、ナビゲーションコントローラが自動的にナビゲーションバーを表示します。ナビゲーションバービューを作成しないでください。 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/index.html –
P.S.を参照してください。あなたのアプリはそれがクールになるように見えます! –