Ios 以编程方式创建和推送视图控制器
我正在创建并按下一个按钮上的视图控制器,使用下面的代码作为附加到按钮的实用方法Ios 以编程方式创建和推送视图控制器,ios,uiviewcontroller,swift,uinavigationcontroller,Ios,Uiviewcontroller,Swift,Uinavigationcontroller,我正在创建并按下一个按钮上的视图控制器,使用下面的代码作为附加到按钮的实用方法 func createSmartController() -> UIViewController{ //Create controller and get view var controller = UIViewController() var view = controller.view var tag
func createSmartController() -> UIViewController{
//Create controller and get view
var controller = UIViewController()
var view = controller.view
var tag:String
var count = 0
//Create and layout scroll view
var scrollView = UIScrollView()
scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
view.addSubview(scrollView)
view.addConstraint(NSLayoutConstraint(item: scrollView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
view.addConstraint(NSLayoutConstraint(item: scrollView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))
view.addConstraint(NSLayoutConstraint(item: scrollView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
view.addConstraint(NSLayoutConstraint(item: scrollView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
//Create and add content view
var scrollContent = UIView()
scrollContent.setTranslatesAutoresizingMaskIntoConstraints(false)
scrollView.addSubview(scrollContent)
scrollView.addConstraint(NSLayoutConstraint(item: scrollContent, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
scrollView.addConstraint(NSLayoutConstraint(item: scrollContent, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))
scrollView.addConstraint(NSLayoutConstraint(item: scrollContent, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
scrollView.addConstraint(NSLayoutConstraint(item: scrollContent, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
view.addConstraint(NSLayoutConstraint(item: scrollContent, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
view.addConstraint(NSLayoutConstraint(item: scrollContent, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
//Create all buttons
var buttons = self.GetMyButtons() //Just returns and array of UIButtons
//Add buttons to view with constraints
var prevButton:String
var constH:NSArray
var constV:NSArray
var hString:String
var vString:String
var index = 0
for button in buttons{
button.setTranslatesAutoresizingMaskIntoConstraints(false)
scrollContent.addSubview(button)
button.bounds.size.height = 90
scrollContent.addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: scrollContent, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
scrollContent.addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: scrollContent, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
if(index == 0){
scrollContent.addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: scrollContent, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 8.0))
}
else{
scrollContent.addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: buttons[index-1], attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 8.0))
}
if(index == buttons.count-1){
scrollContent.addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: scrollContent, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -8.0))
}
index++
}
return controller
}
在下面的调用中使用此方法生成并按下按钮touchupinside上的视图控制器
var controller = button.getSmartController()
self.navigationController?.pushViewController(controller, animated: true)
但是,被推送的视图控制器似乎根本没有视图。它是黑色/空白的。推送动画似乎也有一点问题。所有背景色都不存在,显示为黑色。以编程方式创建元素时,还必须添加样式,否则所有默认值都将返回null/black。我在这里没有看到任何objective-c。错误的标签?@CrimsonChris我接受swift和objective-c作为我的大部分ios内容,因为翻译很简单。你正在寻求帮助调试swift代码。请不要为此使用objective-c标记。@CrimsonChris我将接受使用这两种语言的帮助。您的createSmartController函数没有返回值。您是否找到了出现轻微故障的原因?我也有一个。