Respuesta

14

¿Desea utilizar Interface Builder o hacerlo solo en código?

Con el funcionamiento de IB es muy sencillo, solo tiene que arrastrar el control segmentado para colocarlo en la barra de navegación donde se encuentra el título. El título será reemplazado por el control segmentado.

Si desea lograr esto en el código, consulte this section of iPhone reference library. Parece que necesita establecer la propiedad titleView del elemento de navegación en su control segmentado, que es la subclase de UIView, por lo que esto es completamente legal.

+1

¿le importaría actualizar el enlace, ya que está obsoleto? thx – xon1c

+0

@ xon1c: bien, hecho – madej

1

en viewDidLoad:

obj-c:

NSArray *segmentTitles = @[ 
    @"segment1", 
    @"segment2", 
]; 

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTitles]; 
segmentedControl.selectedSegmentIndex = 0; 
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
// change the width from 400.0 to something you want if it's needed 
segmentedControl.frame = CGRectMake(0, 0, 400.0f, 30.0f); 
[segmentedControl addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged]; 

self.navigationItem.titleView = segmentedControl; 

rápida:

let segmentTitles = [ 
    "segment1", 
    "segment2", 
] 

let segmentedControl = UISegmentedControl(items: segmentTitles) 
segmentedControl.selectedSegmentIndex = 0 
segmentedControl.autoresizingMask = UIViewAutoresizing.FlexibleWidth 
// change the width from 400.0 to something you want if it's needed 
segmentedControl.frame = CGRectMake(0, 0, 400.0, 30.0) 
segmentedControl.addTarget(self, action: "segmentChanged:", forControlEvents: UIControlEvents.ValueChanged) 

self.navigationItem.titleView = segmentedControl 
Cuestiones relacionadas