私はこのiPadアプリに取り組んでおり、自動レイアウトを使用して生活を楽にすることを考えています。このサイドバーコントロールを作成して、ユーザーが別のページに変更できるようにしました(タブバーコントローラーと同じですが、これはiPadの左側にあります)。横向きの場合、このビューの幅を256ピクセルにしますが、iPadが縦向きの場合、このビューの幅を100ピクセルにします。インターフェースの向きに応じて、自動レイアウトを使用してビューの幅を固定するにはどうすればよいですか?
1 に答える
1
updateViewConstraints呼び出しを使用して、向きの変更時にレイアウトを変更できます。
以下の例では、プログラムでビューを作成しますが、インターフェイス ビルダーでサイド バーの幅の制約を結び付けて、同じことを実現できます。
例えば:
//create a custom view using autolayout, this is the equivalent of you side bar
- (void)viewDidLoad{
[super viewDidLoad];
//create a custom view
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
UIView *vw=[[UIView alloc] init];
self.customView =vw;
self.customView.backgroundColor = [UIColor blackColor];
[self.customView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.customView];
NSArray *arr;
//horizontal constraints
arr = [NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[vw]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(vw)];
[self.view addConstraints:arr];
//vertical constraints
arr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[vw(200)]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(vw)];
[self.view addConstraints:arr];
}
- (void)updateViewConstraints{
[super updateViewConstraints];
//remove the existing contraint
if(self.widthConstraint!=nil){
[self.view removeConstraint:self.widthConstraint];
}
//portait set width to 100
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
self.widthConstraint = [NSLayoutConstraint constraintWithItem:self.customView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1.0
constant:100.0];
}
//landscape set width to 256
else{
self.widthConstraint = [NSLayoutConstraint constraintWithItem:self.customView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1.0
constant:256.0];
}
[self.view addConstraint:self.widthConstraint];
}
于 2013-02-11T23:56:32.700 に答える