self.navigationItem.titleView不居中

本文探讨了在iOS应用中如何解决UINavigationBar标题视图无法居中的问题,并提供了两种实用的方法来确保标题始终居中显示。一种是在CustomView中重写setFrame方法,另一种是通过调整UIImageView的位置和大小来实现。

为什么self.navigationItem.titleView有时候不能居中呢

答一:UINavigationBar automatically centers its titleView as long as there is enough room. If the title isn’t centered that means that the title view is too wide to be centered, and if you set the backgroundColor if your UIImageView you’ll see that’s exactly what is happening.

The title view is too wide because that navigation bar will automatically resize the title to hold its content, using -sizeThatFits:. This means that your title view will always be resized to the size of your image.

答二:Sometime inbetween inbetween your view controller’s viewWillAppear: & viewDidAppear:, the navigation bar is repositioning your titleView and resizing it to fit if necessary. It can end up uncentered because the navigation bar is prefering not to resize your titleView to fit between the buttons. It seems to try to center the title view if it’s small enough, but if not will move your titleView off-center, and then I think only as last resort will resize. The effect is that the titleView is taking up all the space between the two buttons, if its textAlignment is centered then it will centered in that space though not the centered with respect to the whole screen. You can see this in your screen shot.

怎么让它居中呢
值得注意的是如果你的titleView是Label什么的 TextAlignment应该设成center 使用sizetofit

答一

It’s possible to start with the titleView being the screen width, then after the navigation bar changes titleView.frame, update it again yourself in the view controller’s viewDidAppear:. Using its adjusted titleView.frame.origin.x and size.width along with the screen width, you can calculate the largest of the left & right margins, then set the origin.x to that, the size.width to the screen width minus that times 2. However that doesn’t take effect until after the pushed view has animated in and the shift afterwards is visible. You could hide the titleView in viewWillAppear: then unhide in viewDidAppear: after centering it, so instead of sliding in with an off-center titleView which is then shifted, it would slide in with no title which then appears.

A better possibility is to make your titleView your own subclass of UIView (or UILabel or whichever) and override setFrame to resize itself to stay centered in its superview. If I ever end up doing this myself I’ll post it here. Or maybe someone else knows another place to change the titleView’s frame after its been updated but before the view slides in without a view subclass.

这种方法我自己亲测可用 CustomView中重写setframe方法 self.navigationItem.titleView = customView

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
self.center = CGPointMake(self.superview.center.x, self.center.y);
}

答二:Just add your image to navigationController.navigationBar

CGRect myImageS = CGRectMake(0, 0, 44, 44);
UIImageView *logo = [[UIImageView alloc] initWithFrame:myImageS];
[logo setImage:[UIImage imageNamed:@”color.png”]];
logo.contentMode = UIViewContentModeScaleAspectFit;
logo.center = CGPointMake(self.navigationController.navigationBar.width / 2.0, self.navigationController.navigationBar.height / 2.0);
logo.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[self.navigationController.navigationBar addSubview:logo];

http://blog.youkuaiyun.com/wsxzk123/article/details/44755787

继续判断分层 internal func addRightBarButtonItem() { let addImage = TPImageLiteral("common_add_unlogin") let addButton = UIButton.init(frame: CGRect(x: 0, y: 0, width: addImage.size.width, height: addImage.size.height)) addButton.setImage(addImage, for: .normal) addButton.addTarget(self, action: #selector(addDeviceButtonClicked(_:)), for: .touchUpInside) addButton.tintColor = .tpbPrimary addButtonmItem = UIBarButtonItem.init(customView: addButton) self.addButton = addButton negativeButtonItem = UIBarButtonItem.init(customView: UIView(frame: CGRectZero)) let changeDisplayImage = TPImageLiteral("deviceList_display_bigPic") changeDisplayButton = UIButton.init(frame: CGRect(x: 0, y: 0, width: changeDisplayImage.size.width, height: changeDisplayImage.size.height)) changeDisplayButton.setImage(changeDisplayImage, for: .normal) changeDisplayButton.addTarget(self, action: #selector(toggleDisplayMode), for: .touchUpInside) changeDisplayButton.tintColor = .tpbPrimary changeDisplayTypeItem = UIBarButtonItem.init(customView: changeDisplayButton); let searchImage = TPImageLiteral("common_search") let searchButton = UIButton.init(frame: CGRect(x: 0, y: 0, width: searchImage.size.width, height: searchImage.size.height)) searchButton.setImage(searchImage, for: .normal) searchButton.addTarget(self, action: #selector(unbindNavigateToSearch), for: .touchUpInside) searchButton.tintColor = .tpbPrimary searchItem = UIBarButtonItem.init(customView: searchButton) self.navigationItem.rightBarButtonItems = [addButtonmItem, negativeButtonItem] }
11-28
import UIKit import SnapKit class DeviceListNewViewController: SurveillanceCommonTableController { // MARK: - 属性 private lazy var titleView: UILabel = { let label = UILabel() label.text = "设备列表" label.font = .tpm20Medium() label.textColor = .tpbTextPrimary label.textAlignment = .center return label }() private lazy var searchBar: TPBSearchBar = { let bar = TPBSearchBar() bar.placeholder = "搜索" bar.backgroundColor = .clear bar.delegate = self return bar }() private lazy var searchContainerView: UIView = { let view = UIView() view.backgroundColor = UIColor(white: 0.96, alpha: 1.0) view.layer.cornerRadius = 22 view.clipsToBounds = true view.addSubview(searchBar) return view }() private lazy var tabScrollView: UIScrollView = { let scrollView = UIScrollView() scrollView.showsHorizontalScrollIndicator = false scrollView.backgroundColor = .clear scrollView.translatesAutoresizingMaskIntoConstraints = false return scrollView }() private lazy var tabButtons: [UIButton] = { return tabButtonTitles.enumerated().map { index, title in let btn = UIButton(type: .custom) btn.tag = index btn.setTitle(title, for: .normal) btn.titleLabel?.font = UIFont.tpr14Regular() btn.setTitleColor(.black, for: .normal) btn.setTitleColor(.tpbPrimary, for: .selected) btn.layer.borderWidth = 1 btn.layer.borderColor = UIColor(white: 0.85, alpha: 1.0).cgColor btn.layer.cornerRadius = 20 btn.contentEdgeInsets = UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16) btn.addTarget(self, action: #selector(onTabTapped(_:)), for: .touchUpInside) return btn } }() private let tabButtonTitles = ["所有设备", "收藏页面", "站点选择"] private var selectedTabIndex = 0 // MARK: - View Setup override func tpbSetupSubviews() { super.tpbSetupSubviews() setupNav() view.addSubview(titleView) view.addSubview(searchContainerView) view.addSubview(tabScrollView) // 初始化按钮选中状态 updateTabSelection(index: selectedTabIndex) // 👇 直接在这里布局 tab 按钮(内联实现,避免找到方法) layoutTabButtonsInScrollView() } private func setupNav() { navigationItem.leftBarButtonItem = tpbCreateLeftBarButtonItem( with: TPImageLiteral("icon_back"), andTarget: self, andAction: #selector(onBack) ) navigationItem.rightBarButtonItem = tpbCreateRightBarButtonItem( with: TPImageLiteral("icon_settings"), andTarget: self, andAction: #selector(onSettings) ) navigationItem.title = "" } override func tpbMakeConstraint() { super.tpbMakeConstraint() titleView.snp.makeConstraints { make in make.top.equalTo(view.safeAreaLayoutGuide).offset(8) make.centerX.equalToSuperview() } searchContainerView.snp.makeConstraints { make in make.top.equalTo(titleView.snp.bottom).offset(16) make.leading.trailing.equalToSuperview().inset(16) make.height.equalTo(44) // 固定高度适配 TPBSearchBar 常见大小 } searchBar.snp.makeConstraints { make in make.edges.equalTo(searchContainerView).inset(UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)) } tabScrollView.snp.makeConstraints { make in make.top.equalTo(searchContainerView.snp.bottom).offset(16) make.leading.trailing.equalToSuperview().inset(0) make.height.equalTo(40) } } // MARK: - 内联布局 Tab 按钮(防止 “Cannot find” 错误) private func layoutTabButtonsInScrollView() { // 清空之前的内容 tabScrollView.subviews.forEach { $0.removeFromSuperview() } var previousButton: UIButton? var currentX: CGFloat = 16 for button in tabButtons { tabScrollView.addSubview(button) // 使用固定 frame 或 SnapKit 设置位置 button.snp.makeConstraints { make in make.centerY.equalToSuperview() make.height.equalTo(36) if let prev = previousButton { make.leading.equalTo(prev.snp.trailing).offset(8) } else { make.leading.equalTo(tabScrollView.contentLayoutGuide).offset(16) } make.width.greaterThanOrEqualTo(80) } previousButton = button currentX += button.intrinsicContentSize.width + 8 } // 手动设置 contentSize(更稳定) DispatchQueue.main.async { var totalWidth: CGFloat = 16 for (i, btn) in self.tabButtons.enumerated() { totalWidth += btn.intrinsicContentSize.width + (i > 0 ? 8 : 0) } totalWidth += 16 self.tabScrollView.contentSize = CGSize(width: totalWidth, height: 40) } } // MARK: - 数据模型构建(仅搭建结构,填充真实数据) private func reloadData() { var tempSectionArray = [TPBTableSectionModel]() let section0 = TPBTableSectionModel() var cellModels = [TPBBaseTableCellModel]() // Cell 1: Tab ScrollView let tabCellModel = TPBBaseTableCellModel.customContent(with: tabScrollView) cellModels.append(tabCellModel) // Cell 2: 占位用的 CollectionView 区域(仅视觉框架) let collectionViewPlaceholder = UIView() collectionViewPlaceholder.backgroundColor = .tpbBackground collectionViewPlaceholder.layer.cornerRadius = 12 collectionViewPlaceholder.clipsToBounds = true // 添加一个简单的提示标签表示“这里是列表” let placeholderLabel = UILabel() placeholderLabel.text = "设备内容区域" placeholderLabel.textColor = .tpbTextSecondaryContent placeholderLabel.font = .tpr14Regular() placeholderLabel.textAlignment = .center placeholderLabel.sizeToFit() collectionViewPlaceholder.addSubview(placeholderLabel) placeholderLabel.snp.makeConstraints { make in make.center.equalToSuperview() } let collectionCellModel = TPBBaseTableCellModel.customContent(with: collectionViewPlaceholder) collectionCellModel.height = TPBTableElementHeight.customHeight(240) cellModels.append(collectionCellModel) section0.cellModelArray = cellModels tempSectionArray.append(section0) sectionArray = tempSectionArray tableView.reloadData() } // MARK: - 生命周期 override func viewDidLoad() { super.viewDidLoad() reloadData() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // 后续绑定 collectionView delegate } // MARK: - 点击事件 @objc private func onBack() { navigationController?.popViewController(animated: true) } @objc private func onSettings() { print("Settings tapped") } @objc private func onTabTapped(_ sender: UIButton) { guard sender.tag != selectedTabIndex else { return } tabButtons[selectedTabIndex].isSelected = false selectedTabIndex = sender.tag sender.isSelected = true // 后续刷新数据 print("Switched to tab: \(selectedTabIndex)") } private func updateTabSelection(index: Int) { for (i, btn) in tabButtons.enumerated() { btn.isSelected = (i == index) } } } // MARK: - TPBSearchBarDelegate(空实现,仅满足协议要求) extension DeviceListNewViewController: TPBSearchBarDelegate { func searchBar(_ searchBar: TPBSearchBar, textDidChange searchText: String) { // TODO: 搜索关键词变化时处理(后续接入 ViewModel) } func searchBarTextDidBeginEditing(_ searchBar: TPBSearchBar) { addTapToDismissKeyboard() } func searchBarTextDidEndEditing(_ searchBar: TPBSearchBar) { removeTapToDismissKeyboard() } func searchBarSearchButtonClicked(_ searchBar: TPBSearchBar) { searchBar.resignFirstResponder() } } // MARK: - Keyboard Dismiss Gesture private extension DeviceListNewViewController { func addTapToDismissKeyboard() { let tap = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard)) tap.cancelsTouchesInView = false view.addGestureRecognizer(tap) objc_setAssociatedObject( self, &AssociatedKeys.searchBarTapGestureKey, tap, .OBJC_ASSOCIATION_RETAIN_NONATOMIC ) } func removeTapToDismissKeyboard() { guard let tap = objc_getAssociatedObject(self, &AssociatedKeys.searchBarTapGestureKey) as? UITapGestureRecognizer else { return } view.removeGestureRecognizer(tap) objc_setAssociatedObject(self, &AssociatedKeys.searchBarTapGestureKey, nil, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } @objc func dismissKeyboard() { searchBar.resignFirstResponder() } } // MARK: - Associated Object Keys private struct AssociatedKeys { static var searchBarTapGestureKey: UInt8 = 0 } 现在这个,给我实现成这样的效果: 屏幕靠上部分显示titleview,然后下面显示searchbar,然后下面显示收藏按钮、所有设备那个scrollview,然后在下面显示一个UIcollectionView,滚动范围就在这个UICollectionView区域,明白我想要的结构了吗?修改代码 import UIKit import SnapKit class DeviceListNewViewController: SurveillanceCommonTableController { // MARK: - 属性 private lazy var titleView: UILabel = { let label = UILabel() label.text = "设备列表" label.font = .tpm20Medium() label.textColor = .tpbTextPrimary label.textAlignment = .center return label }() private lazy var searchBar: TPBSearchBar = { let bar = TPBSearchBar() bar.placeholder = "搜索" bar.backgroundColor = .clear bar.delegate = self return bar }() private lazy var searchContainerView: UIView = { let view = UIView() view.backgroundColor = UIColor(white: 0.96, alpha: 1.0) view.layer.cornerRadius = 22 view.clipsToBounds = true view.addSubview(searchBar) return view }() private lazy var tabScrollView: UIScrollView = { let scrollView = UIScrollView() scrollView.showsHorizontalScrollIndicator = false scrollView.backgroundColor = .clear scrollView.translatesAutoresizingMaskIntoConstraints = false return scrollView }() private lazy var tabButtons: [UIButton] = { return tabButtonTitles.enumerated().map { index, title in let btn = UIButton(type: .custom) btn.tag = index btn.setTitle(title, for: .normal) btn.titleLabel?.font = UIFont.tpr14Regular() btn.setTitleColor(.black, for: .normal) btn.setTitleColor(.tpbPrimary, for: .selected) btn.layer.borderWidth = 1 btn.layer.borderColor = UIColor(white: 0.85, alpha: 1.0).cgColor btn.layer.cornerRadius = 20 btn.contentEdgeInsets = UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16) btn.addTarget(self, action: #selector(onTabTapped(_:)), for: .touchUpInside) return btn } }() private let tabButtonTitles = ["所有设备", "收藏页面", "站点选择"] private var selectedTabIndex = 0 // MARK: - View Setup override func tpbSetupSubviews() { super.tpbSetupSubviews() setupNav() view.addSubview(titleView) view.addSubview(searchContainerView) view.addSubview(tabScrollView) // 初始化按钮选中状态 updateTabSelection(index: selectedTabIndex) // 👇 直接在这里布局 tab 按钮(内联实现,避免找到方法) layoutTabButtonsInScrollView() } private func setupNav() { navigationItem.leftBarButtonItem = tpbCreateLeftBarButtonItem( with: TPImageLiteral("icon_back"), andTarget: self, andAction: #selector(onBack) ) navigationItem.rightBarButtonItem = tpbCreateRightBarButtonItem( with: TPImageLiteral("icon_settings"), andTarget: self, andAction: #selector(onSettings) ) navigationItem.title = "" } override func tpbMakeConstraint() { super.tpbMakeConstraint() titleView.snp.makeConstraints { make in make.top.equalTo(view.safeAreaLayoutGuide).offset(8) make.centerX.equalToSuperview() } searchContainerView.snp.makeConstraints { make in make.top.equalTo(titleView.snp.bottom).offset(16) make.leading.trailing.equalToSuperview().inset(16) make.height.equalTo(44) // 固定高度适配 TPBSearchBar 常见大小 } searchBar.snp.makeConstraints { make in make.edges.equalTo(searchContainerView).inset(UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)) } tabScrollView.snp.makeConstraints { make in make.top.equalTo(searchContainerView.snp.bottom).offset(16) make.leading.trailing.equalToSuperview().inset(0) make.height.equalTo(40) } } // MARK: - 内联布局 Tab 按钮(防止 “Cannot find” 错误) private func layoutTabButtonsInScrollView() { // 清空之前的内容 tabScrollView.subviews.forEach { $0.removeFromSuperview() } var previousButton: UIButton? var currentX: CGFloat = 16 for button in tabButtons { tabScrollView.addSubview(button) // 使用固定 frame 或 SnapKit 设置位置 button.snp.makeConstraints { make in make.centerY.equalToSuperview() make.height.equalTo(36) if let prev = previousButton { make.leading.equalTo(prev.snp.trailing).offset(8) } else { make.leading.equalTo(tabScrollView.contentLayoutGuide).offset(16) } make.width.greaterThanOrEqualTo(80) } previousButton = button currentX += button.intrinsicContentSize.width + 8 } // 手动设置 contentSize(更稳定) DispatchQueue.main.async { var totalWidth: CGFloat = 16 for (i, btn) in self.tabButtons.enumerated() { totalWidth += btn.intrinsicContentSize.width + (i > 0 ? 8 : 0) } totalWidth += 16 self.tabScrollView.contentSize = CGSize(width: totalWidth, height: 40) } } // MARK: - 数据模型构建(仅搭建结构,填充真实数据) private func reloadData() { var tempSectionArray = [TPBTableSectionModel]() let section0 = TPBTableSectionModel() var cellModels = [TPBBaseTableCellModel]() // Cell 1: Tab ScrollView let tabCellModel = TPBBaseTableCellModel.customContent(with: tabScrollView) cellModels.append(tabCellModel) // Cell 2: 占位用的 CollectionView 区域(仅视觉框架) let collectionViewPlaceholder = UIView() collectionViewPlaceholder.backgroundColor = .tpbBackground collectionViewPlaceholder.layer.cornerRadius = 12 collectionViewPlaceholder.clipsToBounds = true // 添加一个简单的提示标签表示“这里是列表” let placeholderLabel = UILabel() placeholderLabel.text = "设备内容区域" placeholderLabel.textColor = .tpbTextSecondaryContent placeholderLabel.font = .tpr14Regular() placeholderLabel.textAlignment = .center placeholderLabel.sizeToFit() collectionViewPlaceholder.addSubview(placeholderLabel) placeholderLabel.snp.makeConstraints { make in make.center.equalToSuperview() } let collectionCellModel = TPBBaseTableCellModel.customContent(with: collectionViewPlaceholder) collectionCellModel.height = TPBTableElementHeight.customHeight(240) cellModels.append(collectionCellModel) section0.cellModelArray = cellModels tempSectionArray.append(section0) sectionArray = tempSectionArray tableView.reloadData() } // MARK: - 生命周期 override func viewDidLoad() { super.viewDidLoad() reloadData() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // 后续绑定 collectionView delegate } // MARK: - 点击事件 @objc private func onBack() { navigationController?.popViewController(animated: true) } @objc private func onSettings() { print("Settings tapped") } @objc private func onTabTapped(_ sender: UIButton) { guard sender.tag != selectedTabIndex else { return } tabButtons[selectedTabIndex].isSelected = false selectedTabIndex = sender.tag sender.isSelected = true // 后续刷新数据 print("Switched to tab: \(selectedTabIndex)") } private func updateTabSelection(index: Int) { for (i, btn) in tabButtons.enumerated() { btn.isSelected = (i == index) } } } // MARK: - TPBSearchBarDelegate(空实现,仅满足协议要求) extension DeviceListNewViewController: TPBSearchBarDelegate { func searchBar(_ searchBar: TPBSearchBar, textDidChange searchText: String) { // TODO: 搜索关键词变化时处理(后续接入 ViewModel) } func searchBarTextDidBeginEditing(_ searchBar: TPBSearchBar) { addTapToDismissKeyboard() } func searchBarTextDidEndEditing(_ searchBar: TPBSearchBar) { removeTapToDismissKeyboard() } func searchBarSearchButtonClicked(_ searchBar: TPBSearchBar) { searchBar.resignFirstResponder() } } // MARK: - Keyboard Dismiss Gesture private extension DeviceListNewViewController { func addTapToDismissKeyboard() { let tap = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard)) tap.cancelsTouchesInView = false view.addGestureRecognizer(tap) objc_setAssociatedObject( self, &AssociatedKeys.searchBarTapGestureKey, tap, .OBJC_ASSOCIATION_RETAIN_NONATOMIC ) } func removeTapToDismissKeyboard() { guard let tap = objc_getAssociatedObject(self, &AssociatedKeys.searchBarTapGestureKey) as? UITapGestureRecognizer else { return } view.removeGestureRecognizer(tap) objc_setAssociatedObject(self, &AssociatedKeys.searchBarTapGestureKey, nil, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } @objc func dismissKeyboard() { searchBar.resignFirstResponder() } } // MARK: - Associated Object Keys private struct AssociatedKeys { static var searchBarTapGestureKey: UInt8 = 0 }
最新发布
12-02
上述代码中,将title的相关设置改为下面代码中写的方法,尤其是设定约束的时候有安全区域那里 import UIKit class AddOrganizationCommonTableController: SurveillanceCommonTableController { // MARK: - 属性 var currentPage = 0 var numberOfPages = 4 var showSkip = false var titleStr = " " var subTitleStr: String? var bottomButtonTitleStr = " " private lazy var pageControlContainerView: UIView = { let view = UIView() view.backgroundColor = .clear return view }() private lazy var pageControl: TPBPageControl = TPBPageControl() private lazy var titleContainerView: UIView = { let view = UIView() view.backgroundColor = .clear return view }() private lazy var titleLabel: UILabel = { let label = UILabel() label.text = titleStr label.numberOfLines = 1 label.font = .tpr22Regular() label.textColor = .tpbTextPrimary label.textAlignment = .left label.lineBreakMode = .byWordWrapping return label }() private lazy var subTitleLabel: UILabel = { let label = UILabel() label.text = subTitleStr label.numberOfLines = 0 label.font = .tpm14Medium() label.textColor = .tpbTextSecondaryContent label.textAlignment = .left label.lineBreakMode = .byWordWrapping return label }() private lazy var bottomButton: TPBButton = { let button = TPBButton.primaryButton(withTitle: bottomButtonTitleStr) return button }() var isBottomButtonEnabled: Bool = false { didSet { bottomButton.isEnabled = isBottomButtonEnabled } } // MARK: - Setup Data (需要在tpbSetupInitialData时候调用) func setupData(currentPage: Int = 0, numberOfPages: Int = 4, showSkip: Bool = false, titleStr: String = " ", subTitleStr: String? = nil, bottomButtonTitleStr: String = " ") { self.currentPage = currentPage self.numberOfPages = numberOfPages self.showSkip = showSkip self.titleStr = titleStr self.subTitleStr = subTitleStr self.bottomButtonTitleStr = bottomButtonTitleStr } // MARK: - View override func tpbSetupSubviews() { super.tpbSetupSubviews() setupAddOrganizationNav() view.addSubviews([titleContainerView, bottomButton]) titleContainerView.addSubviews([titleLabel, subTitleLabel]) subTitleLabel.isHidden = subTitleStr == nil } private func setupAddOrganizationNav() { navigationItem.leftBarButtonItem = tpbCreateLeftBarButtonItem(with: TPImageLiteral("common_light_back_nor"), andTarget: self, andAction: #selector(clickBack)) if showSkip { navigationItem.rightBarButtonItem = tpbCreateRightBarButtonItem(withText: LocalizedString(key: commonSkip), andTextColor: .tpbTextPrimary, andTarget: self, andAction: #selector(clickSkip)) } navigationItem.titleView = pageControlContainerView pageControlContainerView.addSubview(pageControl) pageControl.numberOfPages = numberOfPages pageControl.currentPage = currentPage pageControl.snp.makeConstraints { make in make.edges.equalToSuperview() } } override func tpbMakeConstraint() { // 暂执行super然会显示了title // super.tpbMakeConstraint() titleContainerView.snp.makeConstraints { make in make.top.leading.trailing.equalTo(view.safeAreaLayoutGuide) } titleLabel.snp.makeConstraints { make in make.top.equalToSuperview().offset(6) make.leading.equalToSuperview().offset(20) if subTitleLabel.isHidden { make.bottom.equalToSuperview().offset(-10) } make.trailing.equalToSuperview().offset(-20) } subTitleLabel.snp.makeConstraints { make in make.top.equalTo(titleLabel.snp.bottom).offset(20) make.leading.trailing.equalTo(titleLabel) make.bottom.equalToSuperview().offset(-10) } tableView.snp.remakeConstraints { make in make.top.equalTo(titleContainerView.snp.bottom) make.leading.trailing.equalTo(view.safeAreaLayoutGuide) make.bottom.equalTo(bottomButton.snp.top).offset(-20) } bottomButton.snp.makeConstraints { make in make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-10) make.leading.equalTo(view.safeAreaLayoutGuide.snp.leading).offset(20) make.trailing.equalTo(view.safeAreaLayoutGuide.snp.trailing).offset(-20) make.height.equalTo(44) } } override func tpbBindActions() { super.tpbBindActions() bottomButton.addTarget(self, action: #selector(clickBottomButton), for: .touchUpInside) } // MARK: - 点击事件 @objc func clickBack() { navigationController?.popViewController(animated: true) } @objc private func clickSkip() { // 需要重写 } @objc func clickBottomButton() { // 需要重写 } }
12-02
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值