Ios 附加到屏幕特定部分的按钮
我想添加一个附加到屏幕特定部分的按钮。这意味着当我滚动我的TableView时,这个按钮在屏幕上的位置不会改变。我希望我的屏幕看起来完全像或应用程序 如果有任何帮助,我将不胜感激。谢谢大家! 检查此示例:Ios 附加到屏幕特定部分的按钮,ios,swift,uibutton,autolayout,Ios,Swift,Uibutton,Autolayout,我想添加一个附加到屏幕特定部分的按钮。这意味着当我滚动我的TableView时,这个按钮在屏幕上的位置不会改变。我希望我的屏幕看起来完全像或应用程序 如果有任何帮助,我将不胜感激。谢谢大家! 检查此示例: let button = UIButton(frame: CGRect(x: 0, y: self.view.frame.height - 99, width: self.view.frame.width, height: 50)) button.backgroundColor = UICol
let button = UIButton(frame: CGRect(x: 0, y: self.view.frame.height - 99, width: self.view.frame.width, height: 50))
button.backgroundColor = UIColor(red: 0, green: 88/255, blue: 171/255, alpha: 1)
button.tintColor = .white
button.titleLabel?.font = UIFont(name: "Helvetica", size: 20)
button.setTitle("Search", for: .normal)
button.addTarget(self, action: #selector(search), for: .touchUpInside)
self.navigationController?.view.addSubview(button)
此代码生成一个按钮,并将其放置在表视图上方的导航控制器中如果要添加全局按钮,请使用
class func addGlobalButton() {
let bounds = UIScreen.main.bounds
//Create Button Frame
let button = UIButton(frame: CGRect(x: bounds.width - 90, y: bounds.height - 150, width: 80, height: 80))
//For Circular button
button.clipsToBounds = true
button.layer.cornerRadius = 40
button.backgroundColor = UIColor.red
button.setTitle("+", for: .normal)
button.setTitleColor(UIColor.black, for: .normal)
button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
UIApplication.shared.keyWindow?.addSubview(button)
}
class func buttonClicked() {
print("Button Clicked")
}
将此函数放入app delegate中,并在任何视图控制器中调用一次,如下所示:
AppDelegate.addGlobalButton()
我想,为了这个目的,你可以把按钮添加到导航控制器上。我现在明白了。但如果我没有标签栏呢?如何将此按钮附加到视图底部?您的情况是什么?这是我的和屏幕截图,如果您的意思是这样。因此您可以使用此代码,因为您有导航控制器。如果您没有导航控制器,请检查答案。或者,但按钮应显示在哪里?实施后,我看不到它。我没有导航控制器,因为我可以嵌入其中,如果需要的话,这会使按钮出现在所有vc中的vc上方吗?是的,它会使按钮出现在所有vc上。目前,我只使用一个视图来实现此目的,所以现在使用导航栏的情况更简单。我真的很感谢你的帮助:)如果我以后决定使用你的方法,问题是:我在app delegate中实现了这段代码,但我不能在我的ViewController中调用它:“实例成员'addGlobalButton'不能在'AppDelegate'类型上使用;你是想改为使用这种类型的值吗?”我把它放在我的ViewDidLoad中