Get the correct framework for navigationItem.titleView layoutSubviews

I have a subclass of UIView that I added as a titleView for navigationItem using the following line of code:

 self.navigationItem.titleView = tempview; 

Simple enough. It works great. My problem is that this navigationItem sometimes updated by rightBarButton (sometimes there is no button, sometimes there is one standard size button, sometimes there is a big button).

I realized that I could just use the layoutSubviews method of the layoutSubviews class, which I added as titleView , so I put it in:

 -(void)layoutSubviews { [super layoutSubviews]; self.mylabel.frame = self.bounds; } 

This does not work, because when updating the rightBarButton element rightBarButton it does not change the correct header change.

I also noticed that borders do not grow, as soon as they become smaller, they just change position.

I tried using setNeedsLayout and layoutIfNeeded , but they just "resize" the view with the wrong borders.

I also made sure that the rightBarButton element rightBarButton set to zero, but the view is still not expanding correctly after compression.

Thanks for any help!

+4
source share
3 answers

customize your presentation with

 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth]; 

(possibly in initWithFrame)

then do

 - (void)willMoveToSuperview:(UIView *)newSuperview { [self setFrame:[newSuperview bounds]]; } 

Now you have a view corresponding to the size of the container, and resizing automatically.

+4
source

By default, layoutSubviews does nothing. You also never resize the titleView, so if navbar doesn't do this for you, it's no wonder nothing changes.

Since you only change the size of one of the subzones, I don’t think autosave will work, as I am sure it will work when the supervisor (the one who has autoresizesubviews enabled) resizes. I would suggest recounting the size of the header when you change the right button. If it automatically fits when you add it the first time, you can delete and read it, but I know this is a pretty ugly hack.

+2
source

Instead of overriding -layoutSubviews did you try to set the autoresizingMask property?

 tempView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
0
source

Source: https://habr.com/ru/post/1300226/


All Articles