[RESOLU] problème avec une scroll view: addSubview appelée trop de fois

LouLou Membre
décembre 2014 modifié dans API UIKit #1

Salut,


 


ma scroll view contient 3 views, mais en ajoutant cette scroll view à  la vue principale de mon view controller, la méthode "addSubview" de la scroll view (horizScroll) est appelée à  nouveau une 4e fois, puis une 5e fois. Pourquoi? Je ne comprends pas. Normalement, cette méthode ne devrait être appelée que 3 fois, grâce à  la boucle for.


Le width et height changent alors à  2.5, et le contentSize a donc une taille différente.


 


Voilà  le log :



2014-12-09 01:43:04.629 AEFAutolayout[3845:1028146] view init, frame {{0, 0}, {100, 100}}:
2014-12-09 01:43:04.632 AEFAutolayout[3845:1028146] numSubviews 1 :
2014-12-09 01:43:04.632 AEFAutolayout[3845:1028146] view frame : {{0, 0}, {100, 100}} :
2014-12-09 01:43:04.633 AEFAutolayout[3845:1028146] view init, frame {{50, 0}, {100, 100}}:
2014-12-09 01:43:04.633 AEFAutolayout[3845:1028146] numSubviews 2 :
2014-12-09 01:43:04.634 AEFAutolayout[3845:1028146] view frame : {{100, 0}, {100, 100}} :
2014-12-09 01:43:04.634 AEFAutolayout[3845:1028146] view init, frame {{100, 0}, {100, 100}}:
2014-12-09 01:43:04.635 AEFAutolayout[3845:1028146] numSubviews 3 :
2014-12-09 01:43:04.635 AEFAutolayout[3845:1028146] view frame : {{200, 0}, {100, 100}} :
2014-12-09 01:43:04.636 AEFAutolayout[3845:1028146] contentSize {300, 100}, frame {{0, 0}, {200, 100}} :
2014-12-09 01:43:04.637 AEFAutolayout[3845:1028146] count subviews 4 :
2014-12-09 01:43:04.637 AEFAutolayout[3845:1028146] hello
2014-12-09 01:43:04.646 AEFAutolayout[3845:1028146] numSubviews 4 :
2014-12-09 01:43:04.646 AEFAutolayout[3845:1028146] view frame : {{7.5, 0}, {2.5, 2.5}} :
2014-12-09 01:43:04.648 AEFAutolayout[3845:1028146] numSubviews 5 :
2014-12-09 01:43:04.648 AEFAutolayout[3845:1028146] view frame : {{10, 0}, {2.5, 2.5}} :

Voilà  le code:



@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
HorizScroll *ho = [[HorizScroll alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];

//my view
for ( int i=0; i<3; i++){
MyView* mv = [[MyView alloc] init];
[ho addSubview:mv];
}
//ho.contentSize = CGSizeMake(501.f, 100.f);
NSLog(@contentSize %@, frame %@ : ", NSStringFromCGSize(ho.contentSize),
NSStringFromCGRect(ho.frame) );
[self.view addSubview:ho];
NSLog(@count subviews %d : , [self.view.subviews count]);
NSLog(@hello);
}









@implementation MyView
-(instancetype)init{
if ( self = [super init] ){
NSLog(@view init, frame %@: ", NSStringFromCGRect([self frame]));

CGFloat red = [self getRandomNumberBetween:1 to:255];
self.backgroundColor = [UIColor colorWithRed:red/256.0
green:[self getRandomNumberBetween:1 to:255]/256.0
blue:[self getRandomNumberBetween:1 to:255]/256.0
alpha:1.0];

}
return self;
}

static NSUInteger counterX = 0;

-(instancetype)initWithFrame:(CGRect)frame{
if ( self = [super initWithFrame:frame] ){
self.frame = CGRectMake(counterX, 0, 100, 100);
counterX += 50;










@implementation HorizScroll
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
}
return self;
}

-(void)addSubview:(UIView *)view{ //appelée une 4e et 5e fois? pourquoi?
[super addSubview:view];

NSUInteger numSubviews = self.subviews.count;
NSLog(@numSubviews %d : , numSubviews);
//on place la subview
[view setFrame:CGRectMake(CGRectGetWidth(view.bounds)*(numSubviews-1),
0,
CGRectGetWidth(view.bounds),
CGRectGetHeight(view.bounds) )];

NSLog(@view frame : %@ : ", NSStringFromCGRect(view.frame));
[self setContentSize:CGSizeMake(CGRectGetWidth(view.bounds)*(numSubviews),
CGRectGetHeight(view.bounds) )];

}

Réponses

  • Ton vewDidLoad ajoute bien 3 vues, on voit bien le "hello" affiché après les trois appels.


    Les deux autres sous-vues sont ajoutées après. Ce ne sont manifestement pas des "MyView", c'est autre chose : des boutons ...


  • LouLou Membre
    décembre 2014 modifié #3

    Ben non, j'ai rien d'autre, à  part un uilabel dans le storyboard...  


     


    EDIT: en effet, ce sont des imageViews... en faisant un log description sur la view de addSubview


     


    Je comprends pas encore très bien, peut-être si quelqu'un a la gentillesse de jeter un oeil :)


     


    C'est un simple projet de test : 

  • jpimbertjpimbert Membre
    décembre 2014 modifié #4

    Petit truc en passant ; ce n'est pas une bonne idée de modifier addSubView, il vaut mieux utiliser didAddSubView.


     


    Et pour répondre à  ta question, ce sont manifestement les indicateurs de scrolling qui sont ajoutés à  la scrollView.


  • D'accord, merci, alors sais-tu pourquoi le scroll ne répond pas au touch? En mettant le contentSize depuis le viewController à  1 de width de plus que le width de la frame, ça marche, mais en mettant un petit with de la frame, puis en remplissant le contentSize, qui devient donc plus grand, le touch ne permet pas de scroller la scroll view. ?


  • LeChatNoirLeChatNoir Membre, Modérateur

    Autolayout ?


  • OK, en fait il fallait enlever ces deux images views avec :



    ho.showsHorizontalScrollIndicator = NO; ho.showsVerticalScrollIndicator=NO;

    Merci !


Connectez-vous ou Inscrivez-vous pour répondre.