1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- //
- // UIViewController+SPSBNavigationBar.m
- // 我的社保
- //
- // Created by shanp on 2021/4/21.
- //
- #import "UIViewController+SPSBNavigationBar.h"
- #import <objc/runtime.h>
- #import "SPSBUIGeneralHeader.h"
- #import "UIViewController+SPSBInitialization.h"
- @implementation UIViewController (SPSBNavigationBar)
- - (void)createNavigationBar {
- if (self.spsb_navigationBar) {
- [self.spsb_navigationBar removeFromSuperview];
- self.spsb_navigationBar = nil;
- }
- [self _spsb_setNavigationBar];
- [self.view bringSubviewToFront:self.spsb_navigationBar];
- }
- - (void)_spsb_setNavigationBar {
- UIView *navigationBar = self.spsb_navigationBar = UIView.new;
- navigationBar.backgroundColor = self.spsb_isLightContent ? spsb_FFFFFF_color(0.f) : spsb_FFFFFF_color(1.f);
- [self.view addSubview:navigationBar];
- [navigationBar makeConstraints:^(JXHConstraintMaker *make) {
- make.top.leading.and.trailing.equalTo(0);
- if (@available(iOS 11.0, *)) {
- make.bottom.equalTo(self.view.safetop).offset(jxh_navigationViewHeight());
- } else {
- make.bottom.equalTo(self.view.top).offset(jxh_navigationViewHeight() + jxh_statusBarHeight());
- }
- }];
-
- UIButton *backButton = self.spsb_barBackButton = [self createBackButton];
- [navigationBar addSubview:backButton];
- [backButton makeConstraints:^(JXHConstraintMaker *make) {
- make.leading.equalTo(16);
- make.bottom.equalTo(0);
- make.size.equalTo((CGSize){50, jxh_navigationViewHeight()});
- }];
-
- UILabel *titleLabel = self.spsb_titleLabel =
- [UILabel convenienceWithFont:spsb_mediumFont(16) text:self.title textColor:self.spsb_isLightContent ? spsb_FFFFFF_color(1.f) : spsb_333333_color() textAlignment:NSTextAlignmentCenter];
- [navigationBar addSubview:titleLabel];
- [titleLabel makeConstraints:^(JXHConstraintMaker *make) {
- make.leading.equalTo(80);
- make.trailing.equalTo(-80);
- make.bottom.equalTo(0);
- make.height.equalTo(jxh_navigationViewHeight());
- }];
- }
- - (void)setBackButtonAndTitleColorIsWhite:(bool)isWhite {
- if (isWhite) {
- self.spsb_titleLabel.textColor = spsb_FFFFFF_color(1.f);
- } else {
- self.spsb_titleLabel.textColor = spsb_333333_color();
- }
- if (self.spsb_barBackButton.imageView.image) {
- [self.spsb_barBackButton setImage:isWhite ? jxh_getImage(arrow_back_white) : jxh_getImage(arrow_back_black) state:JXHButtonControlStateNormal];
- }
- if (self.spsb_barBackButton.titleLabel.text) {
- [self.spsb_barBackButton setTitleColor:isWhite ? spsb_FFFFFF_color(1.f) : spsb_3296FB_color() state:JXHButtonControlStateNormal];
- }
- }
- - (UIButton *)spsb_barBackButton {
- return objc_getAssociatedObject(self, @selector(setSpsb_barBackButton:));
- }
- - (void)setSpsb_barBackButton:(UIButton *)spsb_barBackButton {
- objc_setAssociatedObject(self, _cmd, spsb_barBackButton, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- - (UILabel *)spsb_titleLabel {
- return objc_getAssociatedObject(self, @selector(setSpsb_titleLabel:));
- }
- - (void)setSpsb_titleLabel:(UILabel *)spsb_titleLabel {
- objc_setAssociatedObject(self, _cmd, spsb_titleLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- - (UIView *)spsb_navigationBar {
- return objc_getAssociatedObject(self, @selector(setSpsb_navigationBar:));
- }
- - (void)setSpsb_navigationBar:(UIView *)spsb_navigationBar {
- objc_setAssociatedObject(self, _cmd, spsb_navigationBar, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- @end
|