Ios 按钮未显示

Ios 按钮未显示,ios,swift,uibutton,Ios,Swift,Uibutton,我不熟悉swift编码,当我试图通过代码添加按钮时,它不会显示在模拟器中 我将main.storyboard中视图的尺寸更改为375*1114 还有滚动视图和内部视图,尺寸相同。 还有5个集合视图。 这些都是由故事板设置的 我使用的代码是: let fullScreenSize = UIScreen.main.bounds.size let plusButton = UIButton(type: .custom) plusButton.frame = CGRect(x:

我不熟悉swift编码,当我试图通过代码添加按钮时,它不会显示在模拟器中

我将main.storyboard中视图的尺寸更改为375*1114 还有滚动视图和内部视图,尺寸相同。 还有5个集合视图。 这些都是由故事板设置的

我使用的代码是:

    let fullScreenSize = UIScreen.main.bounds.size
    let plusButton = UIButton(type: .custom)
    plusButton.frame = CGRect(x: 300, y: 600, width: 50, height: 50)
    plusButton.layer.cornerRadius = 0.5 * plusButton.bounds.size.width
    plusButton.clipsToBounds = true
    plusButton.setImage(UIImage(named: "plus.png"), for: .normal)
    plusButton.center = CGPoint(x: fullScreenSize.width * 0.9, y: fullScreenSize.height * 0.9)
    plusButton.addTarget(self, action: #selector(plusButtonPressed), for: .touchUpInside)
    self.view.addSubview(plusButton)

我试图让按钮一直停留在模拟器的右下角。我该怎么做?谢谢。

您的代码正确无误。添加背景色并检查按钮是否可见,否则请检查您的
plusButton.setImage(UIImage(名为:“plus.png”),例如:。正常)
plus.png是否正确

plusButton.backgroundColor = UIColor.red
完整代码

let fullScreenSize = UIScreen.main.bounds.size
    let plusButton = UIButton(type: .custom)
    plusButton.frame = CGRect(x: 300, y: 600, width: 50, height: 50)
    plusButton.layer.cornerRadius = 0.5 * plusButton.bounds.size.width
    plusButton.clipsToBounds = true
    plusButton.setImage(UIImage(named: "normal.jpg"), for: .normal)
    plusButton.backgroundColor = UIColor.red
    plusButton.center = CGPoint(x: fullScreenSize.width * 0.9, y: fullScreenSize.height * 0.9)
  //  plusButton.addTarget(self, action: #selector(plusButtonPressed), for: .touchUpInside)
    self.view.addSubview(plusButton)
输出

我认为您必须在
UIView
内部的
uicrollview
中设置一个
UIViewController
的出口,并将该按钮添加为
UIView
子视图。这就是你的按钮隐藏的原因,为什么不通过查看视图层次结构来检查它呢


添加按钮作为ScrollView视图的子视图!尝试使用
self.view.bringSubview(toFront:plusButton)
在调试器中运行应用程序,然后点击“查看调试器”按钮(视频播放约5分钟后,请参阅WWDC 2016)。然后你可以找出它在哪里,从那里你应该可以很容易地调试这个问题。@Rob我想现在它的名字是“调试视图层次结构”。我想你需要将按钮添加到scrollview的子视图(UIView),而不是self.View。我如何才能完成这个过程?谢谢。@szeto1121-pardon@Anbu.Karthik如何将按钮添加到scrollview的子视图?因为当我运行它时,按钮仍然没有显示。我想我需要把它添加到Scrollview@szeto1121-您想将UIbutton置于UIView上的稳定位置,或者您需要ScrollView上的可移动按钮我如何将代码创建的按钮添加到视图层次结构中?谢谢。如果您添加任何UIElement的子视图,它将自动添加到视图层次结构的层中。您是否使用UIView的出口,即在您的UIScrollView内?