如何在右上角以编程方式创建信息按钮?

时间:2011-08-31 18:47:46

标签: iphone objective-c cocoa-touch

我正在尝试这样做,但按钮没有显示:

// Create a Button to get Help          
UIButton *helpButton =  [UIButton buttonWithType:UIButtonTypeInfoDark ] ;
CGRect buttonRect = helpButton.frame;

// Calculate the top right corner
buttonRect.origin.x = self.tableView.frame.size.width - buttonRect.size.width - 8;
buttonRect.origin.y = self.tableView.frame.size.height - buttonRect.size.height - 8; 
[helpButton setFrame:buttonRect];

[helpButton addTarget:self action:@selector(doHelp:)
forControlEvents:UIControlEventTouchUpInside];
[helpButton setEnabled:TRUE];
[self.tableView addSubview:helpButton];

我做错了什么?

更新 修复代码适用于任何感兴趣的人:

// Create a Button to get Help          
UIButton *helpButton =  [UIButton buttonWithType:UIButtonTypeInfoDark ] ;
CGRect buttonRect = helpButton.frame;

// CALCulate the bottom right corner
buttonRect.origin.x = self.view.frame.size.width - buttonRect.size.width - 8;
buttonRect.origin.y = buttonRect.size.height - 8; 
[helpButton setFrame:buttonRect];

[helpButton addTarget:self action:@selector(doHelp:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:helpButton];

1 个答案:

答案 0 :(得分:4)

尝试将其添加到self.view而不是self.tableview