------------- -----------导航
------------- Extra View ------------
- 主视图 - | - 详细信息 -
见图片:Layout
我在Swift 1.2中工作,我希望在我的应用程序中创建上述布局。
在UINavigationController
中,我希望包含UISplitViewController
,并且在UISplitController
之上,我想要一个跨主视图和详细信息视图控制器的标题视图。
在此视图中,我将创建一个带有高级搜索选项的搜索栏。我希望能够编辑这个额外视图的高度,以便它可以有效地下推UISplitViewController
。
以编程方式实现此目标的最佳方法是什么?这是我应该用容器做的事情(它们实际上并不是我以前用过的东西)吗?
由于
答案 0 :(得分:0)
您可以使用UIViewController类型的根视图控制器创建UINavigationController。这为您提供了导航栏。但是,如果你想要的只是一个顶栏,你可以将导航栏添加到UIViewController。
在这个UIViewController中,添加一个固定在顶部,前导和尾随的视图,并给它一个高度。这是图表中的搜索区域。
然后添加一个容器视图以将拆分视图控制器放入。您希望将其顶部固定到搜索视图,前导和尾随以及底部到控制器视图。
将您的UISplitViewController添加为此容器视图的子控件。
这将为您提供图表中的内容。
在几分钟内在故事板中这很简单。要在代码中执行此操作,您需要查看有关如何添加子控制器的文档。 https://developer.apple.com/library/prerelease/ios/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html
更新:在UIViewController中尝试以下代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Create top and bottom views
UIView *topView=[[UIView alloc] init];
UIView *bottomView=[[UIView alloc] init];
// Stop constraints being auto generatyed.
[topView setTranslatesAutoresizingMaskIntoConstraints:NO];
[bottomView setTranslatesAutoresizingMaskIntoConstraints:NO];
// Set the colors so we can see the views.
[topView setBackgroundColor:[UIColor redColor]];
[bottomView setBackgroundColor:[UIColor blueColor]];
// Add the two views
[self.view addSubview:topView];
[self.view addSubview:bottomView];
//
// Add constraints.
//
// Constraint dictionary
NSMutableDictionary *viewDictionary=[NSMutableDictionary dictionaryWithObjectsAndKeys:topView, @"topView",bottomView,@"bottomView", nil];
NSLayoutFormatOptions options;
// Pin leading and trailing of top view
NSArray *constraints=
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[topView]|"
options:options
metrics:nil
views:viewDictionary
];
[self.view addConstraints:constraints];
// Pin leading and trailing of bottom view
constraints=
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[bottomView]|"
options:options
metrics:nil
views:viewDictionary
];
[self.view addConstraints:constraints];
// Pin top view to top, give it height 100, pin bottom to bottom view and bottom
// view to bottom.
constraints=
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topView(100)][bottomView]|"
options:options
metrics:nil
views:viewDictionary
];
[self.view addConstraints:constraints];
//
// Add Split View Controller as child.
//
// Left hand split view controller Temp for now.
UIViewController *left=[[UIViewController alloc] init];
left.view.backgroundColor = [UIColor orangeColor];
// Right hand split view detail contreoller. Temp for now.
UIViewController *right=[[UIViewController alloc] init];
right.view.backgroundColor = [UIColor greenColor];
// Create split view with left and right
UISplitViewController *splitView=[[UISplitViewController alloc] init];
splitView.viewControllers=@[left,right];
// Add as a child controller to the bottom view
[self addChildViewController:splitView];
[bottomView addSubview:splitView.view];
// Add constraints for split view.
viewDictionary=[NSMutableDictionary dictionaryWithObjectsAndKeys:splitView.view,
@"splitView",nil];
// Pin all sides to the container.
constraints=
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[splitView]|"
options:options
metrics:nil
views:viewDictionary
];
[bottomView addConstraints:constraints];
constraints=
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[splitView]|"
options:options
metrics:nil
views:viewDictionary
];
[bottomView addConstraints:constraints];
// Complete contract for adding child controller.
[splitView didMoveToParentViewController:self];
}
答案 1 :(得分:0)
感谢Rory:
let topView = UIView()
let bottomView = UIView()
topView.setTranslatesAutoresizingMaskIntoConstraints(false)
bottomView.setTranslatesAutoresizingMaskIntoConstraints(false)
topView.backgroundColor = UIColor.redColor()
bottomView.backgroundColor = UIColor.blueColor()
self.view.addSubview(topView)
self.view.addSubview(bottomView)
var viewDictionary = Dictionary(dictionaryLiteral: ("topView", topView),("bottomView", bottomView))
var options = NSLayoutFormatOptions()
var constraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|[topView]|", options: nil, metrics: nil, views: viewDictionary)
self.view.addConstraints(constraints)
constraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|[bottomView]|", options: nil, metrics: nil, views: viewDictionary)
self.view.addConstraints(constraints)
constraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|[topView(100)][bottomView]|", options: nil, metrics: nil, views: viewDictionary)
self.view.addConstraints(constraints)
let left = UIViewController()
left.view.backgroundColor = UIColor.orangeColor()
let right = UIViewController()
right.view.backgroundColor = UIColor.greenColor()
let splitView = UISplitViewController()
splitView.viewControllers = [left, right]
self.addChildViewController(splitView)
bottomView.addSubview(splitView.view)
viewDictionary = Dictionary(dictionaryLiteral: ("splitView", splitView.view))
constraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|[splitView]|", options: options, metrics: nil, views: viewDictionary)
bottomView.addConstraints(constraints)
constraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|[splitView]|",options: options, metrics: nil, views: viewDictionary)
bottomView.addConstraints(constraints)
splitView.didMoveToParentViewController(self)
//我出于某种原因现在收到此错误。
2015-10-12 18:20:13.417 GPSApp[36430:478129] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7981a8c0 H:|-(0)-[UILayoutContainerView:0x7981c100] (Names: '|':UIView:0x7983e5e0 )>",
"<NSLayoutConstraint:0x7981a860 H:[UILayoutContainerView:0x7981c100]-(0)-| (Names: '|':UIView:0x7983e5e0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x79815350 h=-&- v=-&- UILayoutContainerView:0x7981c100.midX == UIView:0x7983e5e0.midX + 512>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7981a860 H:[UILayoutContainerView:0x7981c100]-(0)-| (Names: '|':UIView:0x7983e5e0 )>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-10-12 18:20:13.419 GPSApp[36430:478129] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7981a270 V:|-(0)-[UILayoutContainerView:0x7981c100] (Names: '|':UIView:0x7983e5e0 )>",
"<NSLayoutConstraint:0x7981a220 V:[UILayoutContainerView:0x7981c100]-(0)-| (Names: '|':UIView:0x7983e5e0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x798153b0 h=-&- v=-&- UILayoutContainerView:0x7981c100.midY == UIView:0x7983e5e0.midY + 384>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7981a220 V:[UILayoutContainerView:0x7981c100]-(0)-| (Names: '|':UIView:0x7983e5e0 )>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.