2016-04-29 13 views
1

私はビューコントローラの下部にあるツールバーとしてカスタムビューを使用し、自分の方法でデザインしたいと思っています。私はこの目的のためにUIViewを使用しているし、サブビューとして追加します。どういうわけか、私のカスタムツールバーの高さは変わりません。さえ、私はそれのための制約をオフにしますが、どういうわけか、それは自動的に調整します。これどうやってするの?以下は私のコードスニペットです。それは、この問題が発生します使用して、(すなわち_toolbar = [[UIView alloc]initWithFrame:frame];)ビューtoolbarを作成する前にiOSでカスタムUIViewのサイズを変更する方法

//CameraActivity.h class 
@interface CameraActivity : UIViewController 

//Custom UIView. 
@property (nonatomic, strong) UIView *toolbar; 

@end 

//CameraActivity.m class, in viewDidLoad. 

_toolbar.translatesAutoresizingMaskIntoConstraints = YES; 
    [self.view removeConstraints:self.view.constraints]; 
    [_toolbar removeConstraints:_toolbar.constraints]; 
    CGRect frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 44, [[UIScreen mainScreen] bounds].size.width, 150); 
    _toolbar = [[UIView alloc]initWithFrame:frame]; 
    [_toolbar setBackgroundColor:[UIColor colorWithRed:116.0/255.0 green:174.0/255.0 blue:220.0/255.0 alpha:0.75]]; 

    [self.view addSubview:_toolbar]; 
+0

あなたは 'autolayout'を使用していますか? –

+0

はい、1つのビューコントローラでオフにできないためです。 – WasimSafdar

+2

したがって、制約を直接設定し、 'constraint.constant = yourValue'のような' IBOutlet'を使ってその値を変更してください。 –

答えて

0

Constraints need to be setは、あなたのUIView高さ制約の出口を確認します。そして、次のように、デバイスの種類に基づいて、その高さに変更してみてください: - ViewController.hファイルで

を、アウトレット作成: - ViewController.mファイルで

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *viewHeightConstraint; 

を、このようなあなたの高さの制約を変更します -

if (Device is iPhone 4s) { 
    viewHeightConstraint.constant = 30.0; //Your desired height 
} 
else if (Device is iPhone 5 || Device is iPhone 5s || Device is iPhone 5c) { 
    viewHeightConstraint.constant = 40.0; //Your desired height 
} 
else if (Device is iPhone 6) { 
    viewHeightConstraint.constant = 50.0; //Your desired height 
} 
else if (Device is iPhone 6 Plus) { 
    viewHeightConstraint.constant = 60.0; //Your desired height 
} 

ハッピーコーディング!

+0

異なるスクリーンサイズではなく、サイズクラスまたはスクリーンの高さ%でハードコードされた値を使用しないことを強くお勧めします。 –

+0

しかし、あなたのサイズが同じであれば、ハードコードされた値を与える上で問題はないはずです。 –

+0

理論上はい。実際には、たわごとは常に変化します。この種のコードは維持できません。あなたがこのような10のビューを持っていると想像してください、そして、ある日、そのサイズは変わります。新しいiPhoneサイズが現れたとします。アプリがiPadに移植されなければならないと想像してください。それは、OPや他の誰かが助けを求めている間に、この解決策は悪夢です。 –

0

したがって、これに行の順序を変更します。また、私はこのために良い場所ではありませんフレーム必見viewDidLoad beacause -(void) viewDidLayoutSubviewsで行わ設定することをお勧め

CGRect frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 44, [[UIScreen mainScreen] bounds].size.width, 150); 
_toolbar = [[UIView alloc]initWithFrame:frame]; 
_toolbar.translatesAutoresizingMaskIntoConstraints = YES; 
[self.view removeConstraints:self.view.constraints]; 
[_toolbar removeConstraints:_toolbar.constraints]; 
[_toolbar setBackgroundColor:[UIColor colorWithRed:116.0/255.0 green:174.0/255.0 blue:220.0/255.0 alpha:0.75]]; 
[self.view addSubview:_toolbar]; 

! so、

+0

これをviewDidLayoutSubviewsに追加すると、ナビゲーションバーが表示されなくなります。高さは変わらない。あなたのコンピュータでコードを実行しましたか? – WasimSafdar

+0

@WasimSafdarいいえ! I – byJeevan

+0

よろしいですか。私と@ナミトの答えをチェックしてください。 :) – WasimSafdar

0

プログラムで追加する代わりにオブジェクトライブラリからUIViewを追加し、デバイスの画面幅と定数に合わせて幅を設定します。私の問題を解決するためにいくつかの制約が割り当てられました。

//CameraActivity.h class 
@interface CameraActivity : UIViewController 

//This time not custom UIView but IBOutlet. 
@property (nonatomic, strong) IBOutlet UIView *toolbar; 

@end 

//CameraActivity.m class 
#import "CameraActivity.h" 
@implementation CameraActivity 

-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //Get your device screen width. 
    CGFloat width = [UIScreen mainScreen].bounds.size.width; 
    _toolbar.frame = CGRectMake(0, 44, width, 150); 
} 
関連する問題