简体   繁体   中英

y-position not correct at adding a UIView to another UIView

I've added two UIViews to a view with the following code. self.view is my view in the navigation controller.

CGRect rect = [self.view bounds];
CGRect pickerFrame = CGRectMake(0, self.view.frame.size.height - 
                                       self.datePicker.frame.size.height - 44,
                                self.datePicker.frame.size.width,
                                self.datePicker.frame.size.height);
CGRect tableFrame = CGRectMake(0, 0, self.tView.frame.size.width, 
                               self.view.frame.size.height - 
                                   self.datePicker.frame.size.height);
[self.datePicker setFrame:pickerFrame];
[self.tView setFrame:tableFrame];

[self.view addSubview:self.tView];
[self.view addSubview:self.datePicker];

I expected, that the picker was below the tableView , but that was not the result. The result was a 44 pixel width bar between the two objects. What is the reason? I don't understand this issue.

If you want to add picker below the tableview then first set the frame of tableview first and then set the Y position of picker accordingly. Suppose this is your iPhone application and you want to add a table and then a pickerview then your code will be like this -

CGRect tableFrame = CGRectMake(0, 0, self.tView.frame.size.width, (self.view.frame.size.height - self.datePicker.frame.size.height));

[self.tView setFrame:tableFrame];

and then set the frame of pickerview like -

CGRect pickerFrame = CGRectMake(0, self.tView.frame.size.height+2, self.datePicker.frame.size.width, self.datePicker.frame.size.height);
[self.datePicker setFrame:pickerFrame];

and then add these object to your view

[self.view addSubview:self.tView];
[self.view addSubview:self.datePicker];

Thanks

The picker cant be under your tableView because the tableView uses

self.view.frame.size.height - self.datePicker.frame.size.height

as hight
while the y position of you pickerview is smaller

self.view.frame.size.height - self.datePicker.frame.size.height -44

(0, 0) is top left, not bottom left. When you say -44 you are specifying a point 44 pixels above.

Let's say self.view is 150 pixels high and self.datePicker is 50 pixels high.

When you say:

CGRect pickerFrame = CGRectMake(0, self.view.frame.size.height - 
                                       self.datePicker.frame.size.height - 44,
                                self.datePicker.frame.size.width,
                                self.datePicker.frame.size.height);

pickerFrame.origin.y is 150 - 50 - 44 = 56

When you say:

CGRect tableFrame = CGRectMake(0, 0, self.tView.frame.size.width,
                               self.view.frame.size.height - 
                                   self.datePicker.frame.size.height);

tableFrame.size.height is 150 - 50 = 100

56 is lower than 100 so the picker is higher up than the table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM