gpt4 book ai didi

ios - 使用布局 anchor 将自定义导航栏项目居中

转载 作者:行者123 更新时间:2023-11-28 23:54:53 36 4
gpt4 key购买 nike

我已经创建了一个自定义导航项(带有标题和 UISearchBar)来替换 iOS 上的标准导航标题,我正尝试使用布局 anchor 将其置于 UINavigationBar 的中心。

但是,布局约束 [self.navigationItem.titleView.centerXAnchor constraintEqualToAnchor:self.navigationController.navigationBar.centerXAnchor].active = YES; 不起作用,UIView 包含搜索栏的内容显示在 UINavigationBar 的左侧。

其他约束:

  • 宽度应该是屏幕宽度的1/3
  • 我想避免将宽度设置为常量以使 View 响应方向和其他布局更改

如何让导航栏项目居中?

ScreenViewController.m:

- (void) viewDidLoad {
[super viewDidLoad];

// Create the search bar
self.searchBar = [SearchBar new];

// Create a new UIView as titleView (otherwise I'm receiving an error)
self.navigationItem.titleView = [UIView new];

// Add into view hierarchy and turn off auto constraints
[self.navigationController.navigationBar addSubview:self.navigationItem.titleView];
[self.navigationItem.titleView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.navigationItem.titleView addSubview:self.searchBar.view];
[self.searchBar.view setTranslatesAutoresizingMaskIntoConstraints:NO];

// Set anchors for the wrapping view
[self.navigationItem.titleView.widthAnchor constraintEqualToAnchor:self.navigationController.navigationBar.widthAnchor multiplier:1.0/3.0].active = YES;
//[self.navigationItem.titleView.heightAnchor constraintEqualToConstant:50.0].active = YES;
[self.navigationItem.titleView.topAnchor constraintEqualToAnchor:self.navigationController.navigationBar.topAnchor].active = YES;
[self.navigationItem.titleView.centerXAnchor constraintEqualToAnchor:self.navigationController.navigationBar.centerXAnchor].active = YES;
[self.navigationItem.titleView.bottomAnchor constraintEqualToAnchor:self.navigationController.navigationBar.bottomAnchor].active = YES;

// Set anchors for the search bar
[self.searchBar.view.widthAnchor constraintEqualToAnchor:self.navigationItem.titleView.widthAnchor multiplier:1.0].active = YES;
//[self.navigationItem.titleView.heightAnchor constraintEqualToConstant:50.0].active = YES;
[self.searchBar.view.topAnchor constraintEqualToAnchor:self.navigationItem.titleView.topAnchor].active = YES;
[self.searchBar.view.bottomAnchor constraintEqualToAnchor:self.navigationItem.titleView.bottomAnchor].active = YES;
//[self.searchBar.view.leftAnchor constraintEqualToAnchor:self.navigationItem.titleView.leftAnchor].active = YES;
//[self.searchBar.view.rightAnchor constraintEqualToAnchor:self.navigationItem.titleView.rightAnchor].active = YES;
[self.searchBar.view.centerXAnchor constraintEqualToAnchor:self.navigationItem.titleView.centerXAnchor].active = YES;
}

预期结果: Centering search bar

我得到的(标题 View 不居中): Centering search bar problem

查看层次结构:

View hierarchy

最佳答案

您正在做的事情既不可能又违法。您根本没有制作“自定义导航栏项目”(如您的标题所述)。您正在尝试将 subview 直接添加到导航栏。你不能那样做。将某些内容放入导航栏的方法是将其分配到 View Controller 的 navigationItem 中,作为栏按钮项或作为导航项的真实 titleView。 (您将您的变量称为titleView,但实际上您并没有将其设为导航项的titleView。)

关于ios - 使用布局 anchor 将自定义导航栏项目居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51406645/

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