gpt4 book ai didi

ios - UISplitView 与 header 以编程方式快速

转载 作者:行者123 更新时间:2023-11-30 14:00:44 35 4
gpt4 key购买 nike

-------------导航------------

-------------额外 View ------------

--主视图-- | --详细 View --

查看图片:Layout

我正在使用 Swift 1.2,我希望在我的应用程序中创建上述布局。

UINavigationController 中,我想包含一个 UISplitViewController,在该 UISplitController 之上,我想要一个横跨 Master 的标题 View 和详细信息 View Controller 。

在此 View 中,我将创建一个带有高级搜索选项的搜索栏。我希望能够编辑这个额外 View 的高度,以便它可以有效地下推 UISplitViewController

以编程方式实现此目标的最佳方法是什么?这是我应该使用容器来做的事情吗(它们实际上不是我以前使用过的东西)?

谢谢

最佳答案

您可以使用 UIViewController 类型的 Root View Controller 创建 UINavigationController。这将为您提供导航栏。但是,如果您想要的只是顶部栏,则可以将导航栏添加到 UIViewController。

在此 UIViewController 中,添加一个固定在顶部、前导和尾随的 View ,并为其指定高度。这是图表中的搜索区域。

然后添加一个容器 View 来放置 Split View Controller 。您希望将其顶部固定到搜索 View ,将前导和尾随以及底部固定到 Controller View 。

添加您的 UISplitViewController 作为此容器 View 的子 Controller 。

这将为您提供图表中的内容。

这在 Storyboard中只需几分钟即可完成,非常简单。要在代码中执行此操作,您需要查看有关如何添加子 Controller 的文档。 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];
}

关于ios - UISplitView 与 header 以编程方式快速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33038867/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com