0

I'm trying to create a uiview programmatically and adding it to a stack_view which was also programmatically created and added to the view. This is the code:

the .h

@interface ViewController : UIViewController

@property (strong, nonatomic, nullable) UIStackView * stack;

@end

the .m

@implementation viewController

@synthesize stack;

- (void) viewDidLoad {
  [super viewDidLoad];
  CGRect * vframe = self.view.frame;

  // - Option 1
  stack = [[UIStackView alloc] initWithFrame:CGRectMake(vframe.origin.x, 100,vframe.size.width,300)];
  stack.axis = UILayoutConstraintAxisHorizontal;
  stack.aligment = UIStackViewAligmentTop;
  stack.distribution = UIStackViewDistributionFill;
  [self.vew addSubview:stack];

  // Option 2
  // stack.traslateAutorezisingMaskIntoConstraints = NO;
  // [stack.leadingAnchor constraintsEqualToAnchor: self.view.leadingAnchor].active = YES;
  // [stack.topAnchor constraintsEqualToAnchor: self.view.topAnchor constant: 100].active = YES;

  UIView * pView = [[UIView alloc] init];
  pView.backgroundColor = [UIColor blueColor];
  [stack addArrangedSubview:pView];
}

@end

This code does not show the view at all, I've tried to prove the option 2 (appeared commented in the code) and it does not work either. It is not supposed that the view, upon inserted in the stack, will get the size of the stack, since the distribution of it is "Fill"?. None of this work either even if I define Pview with frame=CGRectMakeRect(0,0,self.view.frame.size.width,100), for instance.

What am I doing wrong?

EDIT: I already fix the misspelled in the code (the * in the CGRect and the self.vew instead of self.view). I made these mistakes when I was manually copying the code, I did not copy and paste the code; tha's is why is made them and that's why the original code compile well

Cœur
  • 37,241
  • 25
  • 195
  • 267
Airel
  • 23
  • 8

2 Answers2

0
CGRect vframe = self.view.frame;

In viewDidLoad, your self.view.frame has not been calculated yet. You need to do this in viewDidLayoutSubviews or viewDidAppear. both of these will get called multiple times so be careful.

EDIT:
As suggested below by danh (I overlooked it) you should remove * from the above line , also, you have several misspelling in your code, don't know how this code really even compiled for you.

Community
  • 1
  • 1
  • 1
    Assigning a CGRect struct to a CGRect* probably doesn't compile. Not sure how it got into the OP. – danh Feb 03 '17 at 18:57
  • @danh yeah you are correct, I blindly overlooked it. Also, I noticed he has several misspellings now as "[self.vew addSubview:stack];" in his question, dunno how this compiled for him indeed lol. –  Feb 03 '17 at 18:59
  • thanks danh, my bad...I've already edited the question, these mistakes were not in the original code...thanks anyway dude – Airel Feb 03 '17 at 19:34
  • Thanks for your answer Sneak, the problem is that as you said, the ViewDidLayoutSubviews and viewDidAppear are called several times, and what I need is to just create the view as well as the stack only one time, and preferible at the beggining of the apps. On the other hand, in viewDidLoad method my self.view does already have a frame but it may not be the correct one. I've been thinking that the problem maybe is with the stackView. – Airel Feb 03 '17 at 19:39
  • @Airel You can create the view and everything as you do in viewDidLoad, and you can just do one line of code: stack.frame = self.view.frame , in viewDidLayoutSubviews and it will get the correct size problem solved . :) –  Feb 03 '17 at 19:41
  • @Sneak i'll try what you sugest me. Indeed right now i've tried to create the stack with fixed size, CGRectMake(100,100,100,100), as Andrew McKinley suggest me and it does not work either. – Airel Feb 03 '17 at 19:54
  • I think you should avoid doing this programmatically and do it using AutoLayout and UIStoryboard , if you want to do this programmatically check this thread for a complete code and guide for you: http://stackoverflow.com/questions/30728062/add-views-in-uistackview-programmatically –  Feb 03 '17 at 19:58
  • I've already checked the link you post and that is the answer given in that post (using constraints and so on) was the one I was using before trying to do things this way. The problem is that, in my case, when I get every thing displayed (a stackView with say three uiviews inside) i have to handle gestures on these views. For example, when a view is tapped i must grow that view a certain amount and to displacing the other view according to this amount; all this must be animated. The problem is that , according to apple documentation, the changes in frames may conflict with those constraints. – Airel Feb 03 '17 at 20:35
  • @Airel I have not read the documentation regarding UIStackView , but usually if you have constraints you can just drag the constraints to a NSLayoutConstraint IBOutlet, and you can change the constant in code to change sizes and positions etc. and do this very easy with animations, however, if you say the documentations says that it will conflict doing it this way (sounds wierd that it should) , then I guess I don't know any other solutions and those I wrote and linked you to. –  Feb 03 '17 at 20:37
0

To diagnose this first test the pView. Init it with a fixed size like CGRectMake(100,100,100,100) and add it directly to the viewContoller's view. You should be able to see it no problem if not you have a deeper issue.

If that goes well try the UIStackView with a fixed size. Color its background to see it better if you need. If you still dont see it then double check it still has the correct frame in viewDidAppear of the viewController. It might have adjusted itself after creation. If that is correct go to Debug -> View Debugging -> Capture View Heirachy in Xcode after you have it up in the simulator. If you dont see it there then there was an issue adding it as a subview (note the typo [self.vew addSubview:stack];)

If that goes well then there is a problem with [stack addArrangedSubview:pView]. Similar to the previous step, loop through all the arrangedSubviews in UIStackView in viewDidAppear of its viewController.

Hiren Dhamecha
  • 658
  • 5
  • 15
Andrew McKinley
  • 1,137
  • 1
  • 6
  • 11
  • thanks for your answer Andrew McKinley, I do what you suggest me and these are my findings: 2) when i test only the pview with a fixed size (CGRectMake(100,100,100,100)) and adding it to self.view it works!! b) when I create only the stackView with fixed size (CGRectMake(100,100,100,100)) and adding it to self.view it doesn't work and i've already check that its size is 100 width and 100 of heght in viewDidAppear. the typo you mentioned was not in the original code, I edit the question to fix that mistake. Can you sugest me what to do please? – Airel Feb 03 '17 at 19:44
  • @Airel can you see it in Capture View Heirachy? In viewDidAppear does it have a superview that == self.view? Note we shouldnt add the pView at this point. We are just checking to see if the UIStackView has added to the viewController – Andrew McKinley Feb 03 '17 at 20:06
  • I already see the stack in Capture View Hierarchy and it does a superview that is self.view. I am going to use this same approach to check if Pview is correctly added to the ArrangedSubviews arrays of the stackView when I do [stack addArrangedSubview:pView]; – Airel Feb 03 '17 at 20:26
  • well, i check that and the uiview is already added to the stack but still it is not displayed. I dont know what to do to fix this. I will return to my previous solutions using constraints and I'll check what is the problem with the animations. Thanks for all the ideas dude, best regards!! – Airel Feb 03 '17 at 20:46