5

i want to add an UIView for small size as a subview to existing view.

i have a UIViewController pushed with a nib name which is the main view, i am trying to create an object of UIView and alloc it with dimension with the following code in the viewcontroller's viewDidLoad methoad

UIView *view = [[UIView alloc] init];

but the idea is not suggesting the alloc on UIView and throwing an error if i forcefully type and run

please help me out with this

Johannes Weiss
  • 52,533
  • 16
  • 102
  • 136
Gani
  • 709
  • 3
  • 13
  • 21

3 Answers3

11

To add something (rather important) to the answers above;

CGRect frame = CGRectMake(x, y, width, height); // Replacing with your dimensions
UIView *view = [[UIView alloc] initWithFrame:frame];

Then, you want to actually add it to the superview (assuming the view is self.view)

[self.view addSubview:view];
Jake
  • 3,973
  • 24
  • 36
  • i am trying the same in viewdidload method of the of my viewcontroller class but the xcode is not suggesting alloc on uiview and if i forcefully copy paste this code the view if not being added as a subview to self.view – Gani Sep 16 '09 at 09:18
3

UIView needs to be alloc'ed and init'ed with a frame:

CGRect frame = CGRectMake(x, y, width, height); // Replacing with your dimensions
UIView *view = [[UIView alloc] initWithFrame:frame];
zaph
  • 111,848
  • 21
  • 189
  • 228
  • i am trying the same in viewdidload method of the of my viewcontroller class but the xcode is not suggesting alloc on uiview and if i forcefully copy paste this code the view if not being added as a subview to self.view – Gani Sep 16 '09 at 09:17
0
UIView *view = [[UIView alloc] init];
CVertex
  • 17,997
  • 28
  • 94
  • 124