user403015
user403015

Reputation: 7227

How to add a UIView above a UITableView in iOS?

I am using the xcode template to create a UITableView application with UINavigationController.

I need to add a UIView (at fixed position) between the UINavigationBar and the UITableView. How to do that ?

Thanks.

enter image description here

Upvotes: 5

Views: 2922

Answers (5)

Mahesh Pawar
Mahesh Pawar

Reputation: 57

Step 1) Create a header view as, set outlet to that view.

@property(nonatomic,strong)IBOutlet UIView *viewHeader;

Setp 2) Add following single line code on viewDidLoad

[self.tableview setTableHeaderView:self.viewHeader];

Done!!!

Upvotes: 0

User 123
User 123

Reputation: 19

you need to implement following UITableview delegate methods

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

Upvotes: 1

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73588

Views are maintained in a hierarchy in iOS. They are maintained essentially as an array with the latest view first. To insert some view below one view or above another you just need to manipulate this.

All this is theory, I have not done what you want. So dont hold a grudge against me ;) this is a friendly suggestion.

[self.view insertSubview:yourNewView belowSubview:navigationController];

Upvotes: -1

Mutix
Mutix

Reputation: 4196

You can do this by setting the UITableView's tableHeaderView property.

UITableView class reference

tableHeaderView

Returns an accessory view that is displayed above the table.

@property(nonatomic, retain) UIView *tableHeaderView

Discussion The default value is nil. The table header view is different from a section header.

Availability Available in iOS 2.0 and later.

Upvotes: 6

Ayaz Alavi
Ayaz Alavi

Reputation: 4835

Add UIView and UITableView as subviews of UIView and then position their height, width and x, y position from the Size Inspector in Interface builder or Use CGRectMake in objective-c

Upvotes: 2

Related Questions