How can I get a full-sized UINavigationBar titleView

前端 未结 6 1596
遇见更好的自我
遇见更好的自我 2020-12-25 14:25

I am trying to add a custom control as the titleView in a UINavigationBar. When I do so, despite setting the frame and the properties that would normally assume full width,

6条回答
  •  生来不讨喜
    2020-12-25 14:40

    Just ran into the same problem and after a bit of thinking solved it. titleView's frame gets set by the navigationBar every time it appears.

    All you have to do is subclass UIView and override setFrame.

    Like this:

        - (void)setFrame:(CGRect)frame {
            [super setFrame:CGRectMake(0.0, 0.0, 320.0, 50.0)];
        }
    

    Now set your sneaky new UIView as the navigationItem.titleView and enjoy its newfound resistance to resizing by the superview.

    You don't have to set super's frame every time your frame gets set. You can just set it once and be done. If you want to support orientation changes you could probably hack that together too.

提交回复
热议问题