美文网首页
如何自定义titleView的宽度

如何自定义titleView的宽度

作者: 秦枫桀 | 来源:发表于2017-04-13 16:08 被阅读0次

    前言

    下面的代码,定义了titleView的宽度为屏幕宽度。

    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
    titleView.backgroundColor = [UIColor redColor];
    self.navigationItem.titleView = titleView;
    

    实际效果:


    截图1

    效果并不理想。

    解决方案

    一、创建个UIView类:

    @interface CustomTitleView : UIView
    @end
    
    @implementation CustomTitleView
    
    - (instancetype)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        if (self) {    
        }
        return self;
    }
    
    - (void)setFrame:(CGRect)frame {
        [super setFrame:CGRectMake(0, 0, self.superview.bounds.size.width, self.superview.bounds.size.height)];
    }
    
    @end
    

    二、调用

    UIView *titleView = [[CustomTitleView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
    titleView.backgroundColor = [UIColor greenColor];
    self.navigationItem.titleView = titleView;
    
    

    最终效果

    截图2

    相关文章

      网友评论

          本文标题:如何自定义titleView的宽度

          本文链接:https://www.haomeiwen.com/subject/gzbbattx.html