alekhine
alekhine

Reputation: 1620

Misplacement of contents of UIView

I have a UIView and UIButton (declared in viewDidLoad: method of RootViewcontroller. ).

UIView  *geopointView = [[UIView alloc] initWithFrame:CGRectMake(0, 320, 120, 100)] ;
UIButton *mybutton = [[UIButton alloc] initWithFrame:CGRectMake(0, 330, 120, 30)] ;

mybutton is added to geopointView.

  [geopointView addsubView:mybutton];

Finally UIView is added to RootViewController in method showTheView (defined in rootViewController)

-(void) showTheView{
    [self.view addsubView:geopointView];
}

but when this method is called,I find that both mybutton and geopointView are visible but mybutton is not placed inside of geopointView. mybutton's bounds now appear as CGRectmake(10, 250, 120, 30);

Can anyone please tell what I am missing here. Thanks for any help in advance.

Upvotes: 2

Views: 59

Answers (2)

progrmr
progrmr

Reputation: 77191

The frame of your button should be relative to the parent's bounds, but it appears to be relative to the screen. If you want your button to be inside the geopointView it needs to have x, y coordinates that fit within the width (120) and height (100) of geopointView, so something like this will put it in the top left corner (0,0) of geopointView:

UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,120,30)];

Upvotes: 0

try this

  UIbutton *mybutton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 120, 30)] ;

Upvotes: 1

Related Questions