私はそこにボタンも、ユーザーがボタンを押したとき、それは最初のビューおよびその逆怒鳴るビューにそれを取るような方法で、単一のViewControllerに2つのフルスクリーンUIViewsを持つ方法を探していますあなたはそのためにscrollviewを使用することができます1つのUIVIewControllerに2つのフルスクリーンUIViewを垂直に配置しますか?
答えて
:...シンプルな言葉では、私は単一のUIViewController
に縦に2つのフルスクリーンUIViewsを置きたい私はこのような何かを探していますシナリオ。サブビューとして二つのビューはscrollviewに追加し、ユーザーがボタン(または私の例ではセグメント化されたコントロール)をタップしたときにscrollviewのcontentoffsetを更新:
var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.scrollEnabled = false
view.addSubview(scrollView)
self.scrollView = scrollView
let firstView = UIView()
firstView.translatesAutoresizingMaskIntoConstraints = false
firstView.backgroundColor = UIColor.lightGrayColor()
scrollView.addSubview(firstView)
let secondView = UIView()
secondView.translatesAutoresizingMaskIntoConstraints = false
secondView.backgroundColor = UIColor.darkGrayColor()
scrollView.addSubview(secondView)
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[sv]|", options: [], metrics: nil, views: ["sv": scrollView]))
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[sv]|", options: [], metrics: nil, views: ["sv": scrollView]))
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[fv]|", options: [], metrics: nil, views: ["fv": firstView]))
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[fv][sv]|", options: [.AlignAllLeading, .AlignAllTrailing], metrics: nil, views: ["fv": firstView, "sv": secondView]))
firstView.widthAnchor.constraintEqualToAnchor(scrollView.widthAnchor).active = true
firstView.heightAnchor.constraintEqualToAnchor(scrollView.heightAnchor).active = true
firstView.widthAnchor.constraintEqualToAnchor(secondView.widthAnchor).active = true
firstView.heightAnchor.constraintEqualToAnchor(secondView.heightAnchor).active = true
let segmentedControl = UISegmentedControl(items: ["firstView", "secondView"])
segmentedControl.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(segmentedControl)
segmentedControl.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[sc]-20-|", options: [], metrics: nil, views: ["sc": segmentedControl]))
segmentedControl.selectedSegmentIndex = 0
segmentedControl.addTarget(self, action: #selector(segmentedControlValueChanged(_:)), forControlEvents: .ValueChanged)
}
func segmentedControlValueChanged(segmentedControl: UISegmentedControl) {
scrollView.setContentOffset(CGPointMake(0, CGFloat(segmentedControl.selectedSegmentIndex) * scrollView.frame.height), animated: true)
}
は、私はあなたが右だ願っています。何かが不明な場合はお気軽にお問い合わせください!
UPDATE(OBJ-C)
@interface ViewController()
@property (strong, nonatomic) UIScrollView *scrollView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *scrollView = [UIScrollView new];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
scrollView.scrollEnabled = NO;
[self.view addSubview:scrollView];
self.scrollView = scrollView;
UIView *firstView = [UIView new];
firstView.translatesAutoresizingMaskIntoConstraints = NO;
firstView.backgroundColor = [UIColor lightGrayColor];
[scrollView addSubview:firstView];
UIView *secondView = [UIView new];
secondView.translatesAutoresizingMaskIntoConstraints = NO;
secondView.backgroundColor = [UIColor darkGrayColor];
[scrollView addSubview:secondView];
[NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[sv]|" options:0 metrics:nil views:@{@"sv": scrollView}]];
[NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[sv]|" options:0 metrics:nil views:@{@"sv": scrollView}]];
[NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[fv]|" options:0 metrics:nil views:@{@"fv": firstView}]];
[NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[fv][sv]|" options:NSLayoutFormatAlignAllLeading|NSLayoutFormatAlignAllTrailing metrics:nil views:@{@"fv": firstView, @"sv": secondView}]];
[firstView.widthAnchor constraintEqualToAnchor:scrollView.widthAnchor].active = YES;
[firstView.heightAnchor constraintEqualToAnchor:scrollView.heightAnchor].active = YES;
[firstView.widthAnchor constraintEqualToAnchor:secondView.widthAnchor].active = YES;
[firstView.heightAnchor constraintEqualToAnchor:secondView.heightAnchor].active = YES;
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"firstView", @"secondView"]];
segmentedControl.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:segmentedControl];
[segmentedControl.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = YES;
[NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[sc]-20-|" options:0 metrics:nil views:@{@"sc": segmentedControl}]];
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:@selector(segmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];
}
- (void)segmentedControlValueChanged:(UISegmentedControl *)segmentedControl {
[self.scrollView setContentOffset:CGPointMake(0, segmentedControl.selectedSegmentIndex * CGRectGetHeight(self.scrollView.frame)) animated:YES];
}
@end
ありがとうAndre、任意のチャンスでObj-Cの同じコードベースを持っていますか? –
@NavNav私は客観的なCバージョンで私の答えを更新しました... –
それを感謝します:) –
あなたはUITableView
2の細胞を使用することができ、画面全体を占有して、各セルには、(動的に高さを設定する方法を調べます)。
あなたは明らか独自のクラス&のロジックを持っているでしょう2個のカスタムセル、各ビューのための1つを、持っている可能性があります。働くだろう
は、ビューの一部のみが表示され、または手動でスクロールを制御することを避けるために、ビューの上にスナップのように、様々なscrollviewの利点を使用することができると思います。あなたが好きなだけかもしれない:
また、あなたはビューの数を変更する必要がある場合には、将来の証拠になります!
彼らはscrollviewにいるので、彼らはまた、画面のサイズよりも小さく/大きくてもよいです。
その非常に簡単本当にあなたがautoLayout
を使用して提供。 UIView
をストーリーボードにドラッグアンドドロップし、先頭、末尾、下端、上端をスーパービューに追加します。これをビュー1と呼ぶことができます。別のUIView
をビュー1と同じ階層レベルでストーリーボードにドラッグアンドドロップすると、この新しいビューをビュー2と呼ぶことができます。今すぐ設定することがあなたのVIEW2用サイズインスペクタでオフセットyはあなたのビューの階層構造で今すぐ800
を言うことができます、制御 + VIEW1とドラッグをクリックしてVIEW2上にドロップすると、シフトを押してくださいを選択し、先頭、末尾、垂直スペーシングおよび等高さの制約を選択します。すべての定数が0に設定されていることを確認します(コンテンツに基づいて高さを推測し、フルスクリーンの高さと等しくないようにするには、等高さ制約の優先順位を250に設定します)。
画面は次のようになります。
今すぐあなたのVIEW1を選択し、トップの制約を選択してください。スーパービューに添付されたもの。この制約のコンセントをUIViewController
クラスに作成し、viewOneTopSpaceConstraint
とします。
今あなたがしなければならないすべてはちょうどトグルボタンのIBAction
の内側に、次のとおりです。
if viewOneTopSpaceConstraint.constant == 0{
viewOneTopSpaceConstraint.constant = -UIScreen.main.bounds.size.height
}else{
viewOneTopSpaceConstraint.constant = 0
}
UIView.animate(withDuration: 0.2, animations: {
self.view.layoutIfNeeded()
}
ありがとうございました! –
一つの最後のオプション、私が読んだすべての答えは優れているものの、ユーザーに感じているものを提供することですビューはオフスクリーンで、一方はオンで、基本的にすべてをスーパービューに固定します。ここに私のセットアップとCATransitionsの使用があります。
マスターコンテナはメインビューに固定されていますが、コントロールがそこに移動する予定がある場合は、上部にスペースを追加できます。これは、両方ともmasterContainerに固定されている2つのビューを含んでいます。どちらにも変更を呼び出すボタンがあります。ストーリーボードビュー2にプリセットして、アプリの起動時に非表示にする。ここにコントローラがあります。ビュー1のボタンを押すと、ビュー2が下から上にスライドします。 view2を押すと、view1はview2を元に戻します。ここにコントローラと必要なコードがあります。
import UIKit
class CATransitionDemoViewController: UIViewController {
@IBOutlet weak var masterContainer: UIView!
@IBOutlet weak var view2: UIView!
@IBOutlet weak var view1: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view2.isHidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func viewButton1DidPress(_ sender: Any) {
let transition = CATransition()
transition.duration = 0.4
transition.type = kCATransitionMoveIn
transition.subtype = kCATransitionFromTop
self.masterContainer.layer.add(transition, forKey: nil)
view1.isHidden = !view1.isHidden
view2.isHidden = !view2.isHidden
}
@IBAction func viewButton2DidPress(_ sender: Any) {
let transition = CATransition()
transition.duration = 0.4
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromBottom
self.masterContainer.layer.add(transition, forKey: nil)
view1.isHidden = !view1.isHidden
view2.isHidden = !view2.isHidden
}
ORのObjective Cで
#import "ViewController.h"
@interface ViewController()
@property (weak, nonatomic) IBOutlet UIView *masterContainer;
@property (weak, nonatomic) IBOutlet UIView *view1;
@property (weak, nonatomic) IBOutlet UIView *view2;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[_view2 setHidden:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonView1DidPress:(id)sender {
CATransition *transition = [[CATransition alloc]init];
transition.duration = 0.5;
//or for some real fun uncomment the next line and comment the two lines out after that one
//transition.type = @"suckEffect";
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromTop;
[_masterContainer.layer addAnimation:transition forKey:nil];
[_view1 setHidden:!_view1.hidden];
[_view2 setHidden:!_view2.hidden];
}
- (IBAction)buttonView2DidPress:(id)sender {
CATransition *transition = [[CATransition alloc]init];
transition.duration = 0.5;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromBottom;
[_masterContainer.layer addAnimation:transition forKey:nil];
[_view1 setHidden:!_view1.hidden];
[_view2 setHidden:!_view2.hidden];
}
@end
この方法の利点は、あなたがストーリーボードすべてで作業しているときscrollviewの設定なしに簡単にアクセスできるということです。さらに、CATransitionsにはさらに多くのオプションがあります。
- 1. 2つのEAGLLayerを1つのUIViewに配置する方法
- 2. UIView内にUIViewControllerを配置します
- 3. 2つの要素の間に垂直に配置できません
- 4. 1つの内に4つの異なるdivを垂直に配置します。
- 5. 1つのUIViewに2つのピッカーコントロールを置く
- 6. 2つのdivをオフセットで垂直に整列しますか?
- 7. 1つのブートストラップカラムに垂直のテキスト
- 8. divの中にテキストを垂直に配置しますか?
- 9. フレックスボックス:2つのdivを垂直に中心にします
- 10. 垂直方向に2つの異なるクラスをCSSの隣に配置します
- 11. 中央にグリッドを垂直に配置しますか?
- 12. 右揃えのdivにテキストボックスを垂直に配置します
- 13. UIViewフリップ垂直
- 14. 2つの要素を1つのシーケンスに配置する
- 15. '1つのセルに2つのコマンドボタン'を配置するExcel VBA
- 16. CSSでの垂直配置
- 17. 2つのdivを1つ下に配置する
- 18. 2つのインラインテーブルセルのDIVがそのうちの1つに垂直シフトを引き起こしています
- 19. 垂直リストスタイルイメージをどのように配置できますか?
- 20. C# - 2つの2D配列を1つの3D配列に配置する
- 21. ブートストラップのフォームの2つのフォームグループをインラインに垂直にする
- 22. 2つの大きな列は、水平方向と垂直方向に中央に配置されます
- 23. 2つのdiv間の垂直線スペーサー
- 24. ブートストラップモーダルダイアログの2つの垂直スクロールバー
- 25. 2つのdiv間の垂直距離
- 26. Android:3 TextViews、中央の1つは垂直に配置する必要があります
- 27. 2つのシークスバーを1か所に配置する方法は?
- 28. CSS:水平スクロールバーが固定されたまま、絶対に配置された2つのdivを垂直スクロール
- 29. 垂直方向に2つのdivを中心にする
- 30. UICollectionViewのUICollectionViewCellを垂直にどのように配置しますか?
ビュー間を移動するときに視覚効果を欲しいですか? uiscrollviewは、一度に収まるより多くのビューを表示する一般的な方法です。これにより、(ユーザーのための)スクロールがオフになり、ボタンが押されたときにアプリケーションがスクロール(フルページの上下に)を実行するようにすることができます。 – danh
小さなスクロール効果があまりにも好きではないでしょう –