1

I'm trying to add a view with hight of 50 before table sections, i added the view in interface builder and resize it to 50 and everything looks good in interface builder but once i test it on a device or simulator the view show up with a maximum screen height and it doesn't seem that i can control its height whatever value i use the view takes all the screen hight

How can i force the view height to 50?

Xcode Test Table View Controller

Xcode Test Table View Controller

Xcode Test Table View Controller

Black Frog
  • 11,595
  • 1
  • 35
  • 66
trrrrrrm
  • 11,362
  • 25
  • 85
  • 130

6 Answers6

0

Try to use autolayout and set a height constant.

Pablosproject
  • 1,374
  • 3
  • 13
  • 33
  • doesn't seem i can set a view height constant for a view in autolayout, am i wrong? – trrrrrrm Nov 13 '14 at 14:17
  • @laith.ar You can set a minimum height in a constraint. Have you added any constraints? – Freddy Nov 13 '14 at 14:21
  • You have to add a constant. Just option + click on the view and drag on the same view. You'll be prompted with a selection on the constraint to add to the view. – Pablosproject Nov 13 '14 at 14:22
  • i can't set any autolayout constraints it's all disabled maybe because the UIView is inside a UITable ? – trrrrrrm Nov 13 '14 at 14:27
  • Ok you're right. I figured it out: you can only add a header to your table view with UITableViewControllers. If this is your pourpose, please try to work with the property *Adjust Scroll View Inset*, or set the navigation bar to be opaque and your view will appear. – Pablosproject Nov 13 '14 at 14:36
0

Uncheck "Autoresize Subview" attribute for the parent view in which these view added and check the output. This might solve the problem if some constraint tries to resize the view which was added.

Mithun Ravindran
  • 2,292
  • 2
  • 14
  • 23
  • Please check that you have enabled "size class" to your IB. If yes, then the view which you have placed should be altered to the device you are implementing the code. – Mithun Ravindran Nov 25 '14 at 08:17
0

If you want add and UIView before a table section, this view will be before the tableView also. And At least you do by code this view won't be scrolled.

You must change you TabelViewController to a ViewController and adding the tableView Protocols delegate. And no sure but you must have something like that:

@interface TestTableViewController : UITableViewController

And you should be:

@interface TestTableViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>

Then: In Storyboard add the View and the tableview (Both should have in the same level). Add constrains, connect delegates, and create a IBOutlet for both:

@property (nonatomic,strong) IBOutlet UIView *superHeader;
@property (nonatomic, strong) IBOutlet UITableView *tableView;

With this two outlet if you want you can code to the superHeader disappear when you scrolling until a point, here an example (Remember tableView is a scrollView son).

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  if (scrollView.contentOffset.y > 100)
{
     [UIView animateWithDuration:0.3 animations:^{
        // Be carfuly if you are navigationBar (64 ptos) and depens of transparency property
        self.tableView.frame = CGRectMake(0, -50, self.view.bounds.size.width, 50);
    self.tableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);

    }];
} else if(scrollView.contentOffset.y < 80) {
      [UIView animateWithDuration:0.3 animations:^{
        // Be carfuly if you are navigationBar (64 ptos) and depens of transparency property
        self.tableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 50);
        self.tableView.frame = CGRectMake(0, 50, self.view.bounds.size.width, self.view.bounds.size.height - 50);

    }];

}


}
Onik IV
  • 5,007
  • 2
  • 18
  • 23
0

How did you add the view to the UITableView in Interface Builder?

Do you want to have the view displayed before every section? Then you can add it as a header view for each sections using

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section.

You can specify the height for it using

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

These are both UITableViewDelegate methods.

If you don't want it for every section but rather need one view that is displayed at the top of the TableView, then you can try the tableHeaderView property of UITableView. You can set any UIView as the header using this property. See this StackOverflow thread: Adding iOS UITableView HeaderView (not section header)

Community
  • 1
  • 1
ravi nair
  • 1
  • 1
0

If you want 50 fixed height then add a constraint to the view "fixed height : 50"

The reason for unchanged frame is the auto layout. SetFrame will work fine when you disable the auto layout.

Hope this will help you

Neenu
  • 6,848
  • 2
  • 28
  • 54
0

I feel you are not using the prototypecell of tableViewcontroller. Try to add the customisation into that. It may work.

Satyaranjan
  • 137
  • 1
  • 9