简体   繁体   中英

Adding buttons to top and bottom of UITableView

I have a screen displaying all the matches I'm currently in, in my turn-based game. I am using a table view with 3 sections for this.

Now I want to add a button 'start new game' to the top above the displaying games. Below the displaying games I want a few buttons like 'shop' etc. I can place them but then they won't scroll along so I need to put them in the tableview so they scroll along.

What would be the best way to do this?

UIView *headerContainer = [[UIView alloc]initWithFrame:CGRectMake(X, X, X, X)];
UIButton *btnHeader = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[headerContainer addSubview:btnHeader];


UIView *footerContainer = [[UIView alloc]initWithFrame:CGRectMake(X, X, X, X)];
UIButton *btnFooter = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[footerContainer addSubview:btnFooter];


tblView.tableHeaderView = headerContainer;
tblView.tableFooterView = footerContainer;

I'm not sure exactly where you want your button, but UITableView have a
@property(nonatomic, retain) UIView *tableHeaderView
If you want it to be on the top of your tableView and scroll with it, just make the view you want and put it in that property.

UITableView has a tableHeaderView and a tableFooterView property. You can set this value for your custom view.

@property(nonatomic,retain) UIView *tableHeaderView;                            // accessory view for above row content. default is nil. not to be confused with section header
@property(nonatomic,retain) UIView *tableFooterView;                            // accessory view below content. default is nil. not to be confused with section footer

Note: when you set your custom view to tableHeaderView/tableFooterViewn property, its width will be changed as wide as table view's

add UIbutton to Navigation controller view like this code:

let createButton = UIButton.init(frame: CGRect.init(x: 0, y: self.view.frame.height-50, width: self.view.frame.width, height: 50))
createButton.backgroundColor = UIColor.orange
createButton.setTitle("Create", for: UIControlState())
createButton.addTarget(self, action: #selector(didTapCreate(_:)), for: .touchUpInside)
self.navigationController?.view.addSubview(createButton)

Your question is not that much clear.. in tableview we we gave a propertys property(nonatomic,retain) UIView *tableHeaderView;
@property(nonatomic,retain) UIView *tableFooterView;

and even you can checkout this

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

you can added your view here too.

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

this you can use for the height between the sections

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