2010-06-08 17 views
38

Muy bien, así que la clave aquí es que no estoy usando IB en absoluto, porque la Vista con la que estoy trabajando se crea mediante programación. El UIView cubre la mitad inferior de la pantalla y tiene varios botones. Sin embargo, quiero agregar más botones al UIView, sin agrandarlo. Para hacerlo, quiero hacer un UIScrollView dentro de la vista, lo que me permitirá agregar más botones fuera de la pantalla para que el usuario pueda desplazarse hacia ellos. Creo que así es como funciona.¿Cómo crear un UIScrollView programáticamente?

self.manaView = [[[UIView alloc] initWithFrame:frame] autorelease]; 
self.manaView.backgroundColor = [UIColor purpleColor]; 

UIScrollView *scroll = [UIScrollView alloc]; 
scroll.contentSize = CGSizeMake(320, 400); 
scroll.showsHorizontalScrollIndicator = YES; 
[self.manaView addSubview:scroll]; 

La primera parte del código iniates mi UIView, que funciona muy bien, pero no puedo encontrar la manera de hacer que el UIScrollView programación y añadirlo a la vista, y luego añadir los botones a la misma.

UIButton *ret2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
ret2.tag = 102; 
ret2.frame = CGRectMake(255, 5, 60, 50); 
[ret2 setTitle:@"Return" forState:UIControlStateNormal]; 
[ret2 addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside]; 
[scroll addSubview:ret2]; 

Cuando lo hice, el botón simplemente desapareció de mi pantalla. Entonces, ¿cómo hago esto correctamente? ¡Gracias por tu ayuda!

Respuesta

26

En lugar de:

UIScrollView *scroll = [UIScrollView alloc]; 

hacer esto (ajuste de la trama al por grande que desea que la vista de desplazamiento para ser):

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:...]; 
+1

Ahhh, tenía que tener initwithframe AND .contentSize para que funcione. ¡Gracias! –

23

Esto puede ser útil para usted para crear el UIScrollView mediante programación
http://unconditionalloop.blogspot.com/2011/04/uiscrollview-programmatically.html

UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    NSInteger viewcount= 4; 
    for(int i = 0; i< viewcount; i++) { 

     CGFloat y = i * self.view.frame.size.height; 
     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, y,self.view.frame.size.width, self .view.frame.size.height)];  
     view.backgroundColor = [UIColor greenColor]; 
     [scrollview addSubview:view]; 
     [view release]; 
    }  
    scrollview.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height *viewcount); 
    [self.view addSubview:scrollview]; 
+5

Creo que también necesita hacer '[self.view addSubview: scrollview];' para que funcione. – futurevilla216

+0

@LennyK aparentemente había sido editado de esta manera –

12

Prueba de esto,

{ 

     scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)]; 
     scrollview.showsVerticalScrollIndicator=YES; 
     scrollview.scrollEnabled=YES; 
     scrollview.userInteractionEnabled=YES; 
     [self.view addSubview:scrollview]; 
     scrollview.contentSize = CGSizeMake(width,height); 

    [scrollview release]; 
} 
+1

Utilicé la última opción que respondió NANNAV que parece funcionar bien, pero ahora puedo ver una barra de desplazamiento pero mi UI está bloqueada. Lo que significa que el único control que funciona es UIScrollView ... cualquier idea ... – logixologist

0

Método simple: Puede crear varias veces si necesita más

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    int x = 0; 
    int y = 10; 
    for(int i=0; i<5; i++) 
    { 
     UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 50, 50)]; 
     scrollview.showsVerticalScrollIndicator=YES; 
     scrollview.scrollEnabled=YES; 
     scrollview.userInteractionEnabled=YES; 
     scrollview.backgroundColor = [UIColor greenColor]; 
     [self.view addSubview:scrollview]; 
     //scrollview.contentSize = CGSizeMake(50,50); 
     x=x+55; 

     //[self myscrollView]; 
    } 
} 
0

utilizo lazy mucho al crear la interfaz de usuario mediante programación, así:

class WorldViewController: UIViewController { 
    override func loadView() { 
     super.loadView() 
     view = scrollView 
     scrollView.addSubview(label0) 
    } 

    lazy var scrollView: UIScrollView = { 
     let instance = UIScrollView() 
     instance.backgroundColor = UIColor.blackColor() 
     return instance 
    }() 

    lazy var label0: UILabel = { 
     let instance = UILabel() 
     instance.text = "Ice caps are melting" 
     instance.textColor = UIColor.whiteColor() 
     instance.sizeToFit() 
     return instance 
    }() 

    var needLayoutContent = true 

    override func viewDidLayoutSubviews() { 
     super.viewDidLayoutSubviews() 
     if needLayoutContent { 
      let bounds = scrollView.bounds 
      let contentSize = CGSizeMake(bounds.width * 1.5, bounds.height * 1.5) 
      label0.center = CGPointMake(contentSize.width/2, contentSize.height/2) 
      scrollView.contentSize = contentSize 
      scrollView.contentOffset = CGPointMake(bounds.width * 0.25, bounds.height * 0.25) 
      needLayoutContent = false 
     } 
    } 
} 
3

Crear un UiScrollView Programitically

ScrView = [[UIScrollView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)]; 
ScrView.showsVerticalScrollIndicator=YES; 
[ScrView setBackgroundColor:[UIColor yellowColor]]; 
[ScrView setScrollEnabled:YES]; 
[ScrView setContentSize:CGSizeMake(0, 700)]; 
[ScrView setShowsHorizontalScrollIndicator:YES]; 
[ScrView setShowsVerticalScrollIndicator:YES]; 
[self.view addSubview:ScrView]; 
Cuestiones relacionadas