【问题标题】:Add UIButton above new library ViewController在新库 ViewController 上方添加 UIButton
【发布时间】:2015-05-27 09:11:21
【问题描述】:

我已将第三方库导入到我的项目中,我将其称为原始项目的 appDelegate 中的主 ViewController。我正在尝试添加一个后退按钮(不更改第三方库的代码)。

我在我的 AppDelegate 中向 VC 展示以下内容

ABSViewController *abs = [[ABViewController alloc] init];
    [self.window.rootViewController presentViewController:abs
                                                 animated:NO
                                               completion:nil];

然后我尝试使用下面的程序以编程方式添加我的 UIButton,但即使调用“bringSubViewToFront”它也不会出现在前面

UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
backButton.layer.backgroundColor = [UIColor blueColor].CGColor;

[abs.view addSubview:backButton];
[abs.view bringSubviewToFront:backButton];

我可以看到按钮是在 presentViewController 动画完成之前创建的,但它设置在 ABSViewController(库 ViewController)后面

【问题讨论】:

  • 调用presentViewController后在哪里添加按钮??
  • 这个视图是如何呈现的?如果您使用导航控制器并按下此 VC,则可以设置navigationItem.backBarButtonItem = [UIBarButton alloc] init ...],您将获得此返回按钮。
  • 如果是 VC,你也可以设置self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelPicker)];

标签: ios objective-c xcode xcode6


【解决方案1】:

我认为您需要在presentViewController的完成处理程序中添加按钮

ABSViewController *abs = [[ABViewController alloc] init];
[self presentViewController:viewController animated:YES completion:^{
    UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    backButton.layer.backgroundColor = [UIColor blueColor].CGColor;

    [abs.view addSubview:backButton];
    [abs.view bringSubviewToFront:backButton];
}];

【讨论】:

  • 谢谢,这是问题所在,按钮是在视图之前创建的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-31
  • 2017-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多