Recientemente me enfrento con el mismo problema, tengo una vista de título compleja cuyo contenido puede cambiar, así que quiero mostrar más contenido, el botón izquierdo y el botón derecho tienen un ancho diferente, por lo que Configuré un ancho largo, la barra de navegación no lo centrará. Como es posible que sepa cuando el elemento del botón de la barra izquierda o derecha es muy largo, es imposible centrar la vista del título, por lo que quiero centrarlo tanto como sea posible.
Una vez que establecemos una vista en self.navigationItem.titleView
, la barra de navegación administrará el marco de la vista de título, es difícil centrarlo directamente, por lo que subclase una vista que funciona como vista de contenedor de la vista de título real, configuré su ancho con la pantalla ancho, UINavigationBar
con establecer su marco a un valor de propiedad respecto a su elemento de botón de barra izquierda/derecha.
La vista contenedor es MDLNavigationTitleContainer
y la vista de un título real es MDLMessagesNavigationTitleView
, MDLMessagesNavigationTitleView
es administrado por el diseño de automóviles, ya que es súper vista es MDLNavigationTitleContainer
, cada vez que sus cambios de ancho, layoutSubviews
serán llamados. El código en layoutSubviews
parece un poco extraño, se usa para manejar la animación push/pop.
@interface MDLNavigationTitleContainer()
@property (nonatomic) CGRect oldFrame;
@end
@implementation MDLNavigationTitleContainer
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
UINib *nib = [UINib nibWithNibName:@"MDLMessagesNavigationTitleView" bundle:[NSBundle bundleForClass:[MDLMessagesNavigationTitleView class]]];
self.titleView = [[nib instantiateWithOwner:nil options:nil] firstObject];
self.titleView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:self.titleView];
NSLayoutConstraint *centerX = [NSLayoutConstraint constraintWithItem:self.titleView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
NSLayoutConstraint *centerY = [NSLayoutConstraint constraintWithItem:self.titleView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:self.titleView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:0];
width.priority = 200;
NSLayoutConstraint *width1 = [NSLayoutConstraint constraintWithItem:self.titleView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
[self addConstraint:centerX];
[self addConstraint:centerY];
[self addConstraint:width];
[self addConstraint:width1];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
if (!self.superview) {
return;
}
if (self.center.x > [UIScreen mainScreen].bounds.size.width) {
self.titleView.frame = self.oldFrame;
return;
}
CGFloat centerX = self.center.x;
CGFloat superWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat superCenterX = superWidth/2;
CGFloat offsetX = superCenterX - centerX;
CGPoint center = self.titleView.center;
if (CGRectGetMaxX(self.titleView.frame) + offsetX < self.bounds.size.width &&
CGRectGetMinX(self.titleView.frame) + offsetX > 0) {
center = CGPointMake(self.bounds.size.width/2 + offsetX, self.bounds.size.height/2);
}
CGSize size = self.titleView.bounds.size;
if (size.width > self.bounds.size.width) {
size.width = self.bounds.size.width;
}
self.titleView.frame = CGRectMake(center.x-size.width/2, center.y-size.height/2, size.width, size.height);
self.oldFrame = self.titleView.frame;
}
Establecer el título de la vista:
MDLNavigationTitleContainer *titleView = [[MDLNavigationTitleContainer alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
self.navigationItem.titleView = titleView;
alguien? alguien ayuda! –
¡Estoy teniendo el mismo problema! Los botones en el lado izquierdo o derecho están causando la recentered de titleView, incluso después de que lo configuré exactamente en layoutSubviews – quantumpotato