私は、UISegmentedControlのさまざまな状態を使用してビューを切り替える方法を理解しようとしています。これは、Appleが「TopPaid」と「TopFree」の間を切り替えたときにAppStoreで行う方法と似ています。
9 に答える
最も簡単な方法は、2 つのビューを用意し、それらの表示を切り替えてどちらのビューが選択されているかを示すことです。ビューを処理するための最適化された方法ではありませんが、UISegmentControlを使用して表示ビューを切り替える
方法を示すためのサンプル コードを次に示します。
- (IBAction)segmentSwitch:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
if (selectedSegment == 0) {
//toggle the correct view to be visible
[firstView setHidden:NO];
[secondView setHidden:YES];
}
else{
//toggle the correct view to be visible
[firstView setHidden:YES];
[secondView setHidden:NO];
}
}
もちろん、コードをさらにリファクタリングして、正しいビューを非表示/表示することもできます。
私の場合、私のビューは非常に複雑で、さまざまなビューの非表示のプロパティを変更することはできません。これは、メモリを消費しすぎるためです。
私はいくつかの解決策を試しましたが、それらのどれもうまくいかなかったり、ビューをプッシュ/ポップするときに常にセグメント化されたコントロールを表示しない navBar の titleView で不規則に実行されたりしました。
適切な方法でそれを行う方法を説明する問題に関するこのブログ投稿を見つけました。彼は、WWDC'2010 で Apple エンジニアの助けを借りて、このソリューションを考え出したようです。
http://redartisan.com/2010/6/27/uisegmented-control-view-switching-revisited
このリンクの解決策は、これまでにこの問題について見つけた最良の解決策です。少し調整すると、下部のタブバーでも問題なく動作しました
または、テーブルの場合は、テーブルをリロードし、cellForRowAtIndex で、選択したセグメント オプションに基づいて、さまざまなデータ ソースからテーブルにデータを入力できます。
1 つのアイデアは、セグメント化されたコントロールを含むビューに、さまざまなサブビューで埋めるコンテナー ビューを持たせることです (セグメントが切り替えられたときに、コンテナー ビューの唯一のサブビューとして追加します)。これらのサブビューに対して個別のビュー コントローラーを使用することもできますが、必要に応じて「viewWillAppear」や「viewWillDisappear」などの重要なメソッドを転送する必要があります (また、それらがどのナビゲーション コントローラーの下にあるかを通知する必要があります)。
一般に、IB のコンテナーを使用してメイン ビューをレイアウトでき、サブビューはコンテナーが許可するスペースを埋めることができるため、かなりうまく機能します (自動サイズ変更マスクが適切に設定されていることを確認してください)。
SNFSegmentedViewController
のようなセットアップで探していることを正確に実行するオープンソース コンポーネントであるを使用してみてくださいUITabBarController
。
@Ronnie Liew の回答から、これを作成します。
//
// ViewController.m
// ResearchSegmentedView
//
// Created by Ta Quoc Viet on 5/1/14.
// Copyright (c) 2014 Ta Quoc Viet. All rights reserved.
//
#define SIZE_OF_SEGMENT 56
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize theSegmentControl;
UIView *firstView;
UIView *secondView;
CGRect leftRect;
CGRect centerRect;
CGRect rightRect;
- (void)viewDidLoad
{
[super viewDidLoad];
leftRect = CGRectMake(-self.view.frame.size.width, SIZE_OF_SEGMENT, self.view.frame.size.width, self.view.frame.size.height-SIZE_OF_SEGMENT);
centerRect = CGRectMake(0, SIZE_OF_SEGMENT, self.view.frame.size.width, self.view.frame.size.height-SIZE_OF_SEGMENT);
rightRect = CGRectMake(self.view.frame.size.width, SIZE_OF_SEGMENT, self.view.frame.size.width, self.view.frame.size.height-SIZE_OF_SEGMENT);
firstView = [[UIView alloc] initWithFrame:centerRect];
[firstView setBackgroundColor:[UIColor orangeColor]];
secondView = [[UIView alloc] initWithFrame:rightRect];
[secondView setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:firstView];
[self.view addSubview:secondView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)segmentSwitch:(UISegmentedControl*)sender {
NSInteger selectedSegment = sender.selectedSegmentIndex;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
if (selectedSegment == 0) {
//toggle the correct view to be visible
firstView.frame = centerRect;
secondView.frame = rightRect;
}
else{
//toggle the correct view to be visible
firstView.frame = leftRect;
secondView.frame = centerRect;
}
[UIView commitAnimations];
}
@end