如何使用一个UISegmentedControl切换视图?
-
20-08-2019 - |
题
我想弄清楚如何使用一个UISegmentedControl的不同状态来切换视图,类似苹果的付费顶“和“热门免费”之间switiching时是怎么做的在App Store中。
解决方案
的最简单的方法是具有可以切换它们的可见性,以指示哪个视图已经被选择两个视图。下面是它如何做,绝对不是一种优化的方式来处理的意见,但一些示例代码只是为了演示如何使用的 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];
}
}
,点击 可以当然进一步重新因子的代码隐藏/显示右视图。
其他提示
在我的情况,我的观点是非常复杂的,我不能只是改变的不同意见的隐藏属性,因为它会占用太多的内存。
我试过几种解决方案,并将其非为我工作,或无法正常进行,特别是与导航栏并不总是显示segmentedControl入栈/视图时的titleview的。
我发现这个博客文章,解释如何做到这一点的正确方法的问题。似乎他有苹果公司的工程师在WWDC'2010援助想出这个解决方案。
http://redartisan.com/2010/6/ 27 / uisegmented控制 - 视图 - 切换重新
在这个环节的解决方案是手了最好的解决方案,我迄今发现有关问题。随着调整的一点点,也能正常工作用的TabBar在底部
或者,如果它的一个表,则可以重新加载表,并在cellForRowAtIndex,基于段的选项从不同数据源填充该表中选择。
一个想法是具有与分段控件的视图已您与不同子视图填充容器视图(当段被切换添加作为容器视图的鞋底子视图)。你甚至可以对那些子视图单独视图控制器,虽然你有,如果你需要他们(以及他们将被告知他们是什么导航控制器下),像“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
分配.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];
}
}
夫特版本:
在父视图控制器负责设置每个子视图控制器的视图的大小和位置。子视图控制器的视图变为父视图控制器的视图层次结构的一部分。
定义延迟属性:
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 - 视图 - 控制器/
:一种快速夫特版本:强>
@IBAction func segmentControlValueChanged(_ sender: UISegmentedControl) {
if segmentControl.selectedSegmentIndex == 0 {
// do something
} else {
// do something else
}
}