Building a titleView programmatically with constraints (or generally constructing a view with constraints)

后端 未结 6 1803
长情又很酷
长情又很酷 2021-02-03 20:44

I\'m trying to build a titleView with constraints that looks like this:

\"titleView\"

I know how I would do

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-03 21:31

    For combining auto-layout constraints inside titleView and hardcoded layout logic inside UINavigationBar you have to implement method sizeThatFits: inside your own custom titleView's class (subclass of UIView) like this:

    - (CGSize)sizeThatFits:(CGSize)size
    {
        return CGSizeMake(
            CGRectGetWidth(self.imageView.bounds) + CGRectGetWidth(self.labelView.bounds) + 5.f /* space between icon and text */,
            MAX(CGRectGetHeight(self.imageView.bounds), CGRectGetHeight(self.labelView.bounds))
        );
    }
    

提交回复
热议问题