如何使用一个UISegmentedControl切换视图?如何使用一个UISegmentedCont

2019-05-13 21:12发布

我试图找出如何使用一个UISegmentedControl的不同状态来切换视图,类似苹果的热门付费“和“热门免费”之间switiching时是怎么做的在App Store。

Answer 1:

最简单的办法是有,你可以切换它们的可见性来指示图已经选择了两个观点。 下面是它如何做,绝对不是一种优化的方式来处理的意见,但只是为了演示如何使用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];
  }
}


你当然可以进一步重新因子代码隐藏/显示右视图。



Answer 2:

在我的情况,我的观点是非常复杂的,我不能只是改变的不同意见的隐藏属性,因为它会占用太多的内存。

我试过几种解决方案,并将其非为我工作,或无法正常进行,特别是与导航栏并不总是显示segmentedControl入栈/视图时的titleview的。

我发现这个博客文章,解释如何做到这一点的正确方法的问题。 似乎他有苹果公司的工程师在WWDC'2010的援助想出这个解决方案。

http://redartisan.com/2010/6/27/uisegmented-control-view-switching-revisited

在这个环节的解决方案是手了最好的解决方案,我迄今发现有关问题。 随着调整的一点点,也工作得很好用的TabBar底部



Answer 3:

或者,如果它是一个表,你可以重新加载表和cellForRowAtIndex,填充基于选择的细分选项不同数据源的表。



Answer 4:

一种想法是具有考虑到与分段控件具有你与不同子视图填充容器视图(添加作为容器视图的鞋底子视图当段被交替变化)。 你甚至可以对那些子视图单独视图控制器,虽然你有,如果你需要他们(以及他们将被告知他们是什么导航控制器下),像“viewWillAppear中”和“viewWillDisappear”重要的方法来转发。

普遍认为工作得很好,因为你可以在IB容器铺陈主视图和子视图将填充任何空间的容器可以让他们有(请确保您的自动调整大小掩码设置正确)。



Answer 5:

试试这个代码,这将帮助您对分段控制的变化段不同视图之间切换

关于选择UISegmentControl的不同段开放不同视图



Answer 6:

尝试使用SNFSegmentedViewController ,这不正是你要寻找具有类似的设置一个开放源码组件UITabBarController



Answer 7:

从@Ronnie刘氏的答案,我创建这个:

//
//  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


Answer 8:

分配在.H

 UISegmentedControl *lblSegChange;

- (IBAction)segValChange:(UISegmentedControl *) sender

声明.M

- (IBAction)segValChange:(UISegmentedControl *) sender
{

 if(sender.selectedSegmentIndex==0)
 {
  viewcontroller1 *View=[[viewcontroller alloc]init];
  [self.navigationController pushViewController:view animated:YES];
 }
 else 
 {
  viewcontroller2 *View2=[[viewcontroller2 alloc]init];
  [self.navigationController pushViewController:view2 animated:YES];
 }
} 


Answer 9:

斯威夫特版本:

父视图控制器负责设置每个子视图控制器的视图的大小和位置。 子视图控制器的看法变得父视图控制器的视图层次结构的一部分。

定义延迟属性:

private lazy var summaryViewController: SummaryViewController = {
   // Load Storyboard
   let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

   // Instantiate View Controller
   var viewController = storyboard.instantiateViewController(withIdentifier: "SummaryViewController") as! SummaryViewController

   // Add View Controller as Child View Controller
   self.add(asChildViewController: viewController)

   return viewController
}()

private lazy var sessionsViewController: SessionsViewController = {
    // Load Storyboard
    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

    // Instantiate View Controller
    var viewController = storyboard.instantiateViewController(withIdentifier: "SessionsViewController") as! SessionsViewController

    // Add View Controller as Child View Controller
    self.add(asChildViewController: viewController)

    return viewController
}()

显示/隐藏子视图控制器:

private func add(asChildViewController viewController: UIViewController) {
    // Add Child View Controller
    addChildViewController(viewController)

    // Add Child View as Subview
    view.addSubview(viewController.view)

    // Configure Child View
    viewController.view.frame = view.bounds
    viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    // Notify Child View Controller
    viewController.didMove(toParentViewController: self)
}

private func remove(asChildViewController viewController: UIViewController) {
    // Notify Child View Controller
    viewController.willMove(toParentViewController: nil)

    // Remove Child View From Superview
    viewController.view.removeFromSuperview()

    // Notify Child View Controller
    viewController.removeFromParentViewController()
}

管理SegmentedControl tapEvent

private func updateView() {
    if segmentedControl.selectedSegmentIndex == 0 {
        remove(asChildViewController: sessionsViewController)
        add(asChildViewController: summaryViewController)
    } else {
        remove(asChildViewController: summaryViewController)
        add(asChildViewController: sessionsViewController)
    }
}

当然,你可以向你的孩子视图控制器类中使用:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    print("Summary View Controller Will Appear")
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    print("Summary View Controller Will Disappear")
}

参考: https://cocoacasts.com/managing-view-controllers-with-container-view-controllers/



Answer 10:

快速斯威夫特版本:

@IBAction func segmentControlValueChanged(_ sender: UISegmentedControl) {

    if segmentControl.selectedSegmentIndex == 0 {

        // do something
    } else {

        // do something else
    }
}


文章来源: How do I use a UISegmentedControl to switch views?