我有一个自定义UINavigationBar标题和一个自定义后退按钮.我的问题是标题不是以iPhone为中心.就好像我的后退按钮将标题推到了右边.任何想法我怎么能集中它?
int height = self.navigationController.navigationBar.frame.size.height;
int width = self.navigationController.navigationBar.frame.size.width;
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width + 300, 20)];
navLabel.backgroundColor = [UIColor whiteColor];
navLabel.textColor = [UIColor redColor];
navLabel.font = [UIFont fontWithName:@"Helvetica" size:30];
navLabel.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = navLabel;
[navLabel release];
((UILabel *)self.navigationItem.titleView).text = self.title;
Run Code Online (Sandbox Code Playgroud)
谢谢!
编辑我删除了多余的代码并添加了这张图片:

注意标题是如何被推过来容纳按钮的....
我读了 如何垂直居中 UICollectionView 内容 但是当我在这里使用代码时
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
let navBarHeight = self.navigationController?.navigationBar.frame.height
let collectionViewHeight = (self.collectionView?.frame.height)! - navBarHeight!
let itemsHeight = self.collectionView?.contentSize.height
let topInset = ( collectionViewHeight - itemsHeight! ) / 4
return UIEdgeInsetsMake(topInset ,0, 0 , 0)
}
Run Code Online (Sandbox Code Playgroud)
但是当滚动集合视图时,这将显示不正确的形式,所以这是我的代码,因为我的集合视图单元格是方形的并且等于屏幕宽度
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
showImagesCV.contentInset = UIEdgeInsets(top: spacing, left: spacing, bottom: spacing, right: spacing)
if let flowLayout = showImagesCV.collectionViewLayout as? UICollectionViewFlowLayout{
cellWidth = UIScreen.main.bounds.width
cellHeight = cellWidth //yourCellHeight = …Run Code Online (Sandbox Code Playgroud) ios uicollectionview uicollectionviewcell uicollectionviewlayout swift4