6

I must do something wrong, but dont know what..

I try to add a subView with this code:

subMenuView = [[UISubMenuViewMainController alloc] init];

[subMenuView.view setFrame:CGRectMake(10,0,990,100)];

subMenuView.view.backgroundColor  = [UIColor whiteColor];

[self.view addSubview:subMenuView.view];

I want my view to be at (10,0) and have 990/100 in width/height

but i dont get the expected result

Let me if I m wrong, If I want a 10x10 square view at the center i have to add the following line:

[subMenuView.view setFrame:CGRectMake(512,384,10,10)];

That s not what I get, the position is correct, but the width/height are wrong, any ideas?

Nico AD
  • 1,657
  • 4
  • 31
  • 51

4 Answers4

11

if you use autolayout,the call setFrame have no use,try call setTranslatesAutoresizingMaskIntoConstraints before setFrame

user1923254
  • 119
  • 1
  • 3
7

problem fixed by setting

self.view.autoresizesSubviews = NO;
Nico AD
  • 1,657
  • 4
  • 31
  • 51
0

The code is a little unconventional, but I'll hazard a guess it's because you are setting the view's frame before you add it as a subview. Hence the subview's position probably gets changed by layoutSubviews before you see it.

So try putting the second line of code last and see if that does the trick.

Andrew Ebling
  • 10,175
  • 10
  • 58
  • 75
  • I get exactly the same problem with setFrame and the end of the code snippet – Nico AD Jul 30 '11 at 20:43
  • I dont undestand because I m adding a lot of other controls (UIButton) with this method before and all works perfect. – Nico AD Jul 30 '11 at 20:45
-1

Try using floats instead of ints:

[subMenuView.view setFrame:CGRectMake(10,0,990,100)];

to:

[subMenuView.view setFrame:CGRectMake(10.0f,0.0f,990.0f,100.0f)];
Rui Peres
  • 25,741
  • 9
  • 87
  • 137