【问题标题】:UIButton is not appear on view in iOS 9UIButton 未出现在 iOS 9 中的视图中
【发布时间】:2015-11-26 10:47:43
【问题描述】:

我已经在 iOS 8 中创建了我的项目,它工作正常,现在我尝试在 iOS9 中运行。UIButton 没有出现。我以编程方式创建了一个按钮,但它不仅仅出现在 iOS9 中。它在 iOS7 和 iOS8 中运行良好。请看下面的代码。

    UIButton *NewOrderButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [NewOrderButton addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [NewOrderButton setTitle:@"Create" forState:UIControlStateNormal];
    NewOrderButton.frame = CGRectMake((self.view.frame.size.width-200)/2,(self.view.frame.size.height+80)/2, 200, 40.0);
    [NewOrderButton.layer setBorderWidth:1];
    [NewOrderButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    NewOrderButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.view addSubview:NewOrderButton];

如果我调试这段代码,那么按钮就会出现。

【问题讨论】:

  • 您在视图控制器上的何处添加了此代码?在 viewDidLoad 方法中?
  • 不是_NewOrderButton 它是NewOrderButton
  • 对不起。请检查我编辑了我的问题
  • @Cong Tran 我在 viewDidAppear 中添加了
  • 请检查我的答案。希望对您有所帮助。

标签: ios objective-c uibutton ios9


【解决方案1】:

我在我的环境中运行了这段代码。只需更改以下

[self.view addSubview:_NewOrderButton];

[self.view addSubview:NewOrderButton];

【讨论】:

  • 你检查过 iPad
  • 我在 iPad 中检查了您的代码,我没有遇到任何问题,您的代码完全正确
  • @Anbu.Karthik 我已经在 iOS 8 中创建了我的项目,它工作正常,现在我正在尝试在 iOS9 中运行。UIButton 没有出现
  • @elavarasan -- 我在 iPad air 上检查了 iOS 9 及更高版本,它位于 VC 的中心,如果您有疑问,请设置按钮的背景颜色并检查一次兄弟
  • @Anbu.Karthik 请点击下面的链接我面临同样的问题stackoverflow.com/questions/32976447/ios-9-view-not-appearing
【解决方案2】:

我在 iOS 9 中运行它,它运行良好..

并用这些更改代码的最后一行..

[self.view addSubview:NewOrderButton];

希望对你有帮助..

【讨论】:

    【解决方案3】:

    你应该把你的代码添加到

    -(void)viewDidLayoutSubviews {
        UIButton *NewOrderButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [NewOrderButton addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [NewOrderButton setTitle:@"Create" forState:UIControlStateNormal];
        NewOrderButton.frame = CGRectMake((self.view.frame.size.width-200)/2,(self.view.frame.size.height+80)/2, 200, 40.0);
        [NewOrderButton.layer setBorderWidth:1];
        [NewOrderButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
        NewOrderButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [self.view addSubview:NewOrderButton];
    }
    

    【讨论】:

      【解决方案4】:

      Elavarasan 我在 viedDidLoad 和 viewDidLayoutSubviews 中应用了您的编码,它工作正常。另外请设置按钮的背景颜色。下面是代码。再次在您的课程中应用此代码。一定会得到解决方案。

      UIButton *NewOrderButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      [NewOrderButton addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
      [NewOrderButton setTitle:@"Create" forState:UIControlStateNormal];
      NewOrderButton.backgroundColor = [UIColor blueColor];
      NewOrderButton.frame = CGRectMake((self.view.frame.size.width/250)/2,(self.view.frame.size.height+80)/2, 200, 40.0);
      [NewOrderButton.layer setBorderWidth:1];
      [NewOrderButton.layer setBorderColor:[[UIColor greenColor] CGColor]];
      NewOrderButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
      [self.view addSubview:NewOrderButton];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-19
        • 1970-01-01
        • 2016-07-07
        • 1970-01-01
        • 2018-03-23
        相关资源
        最近更新 更多