iOS标题偏移navigationItem,titleLabel

2023-10-05  本文已影响0人  你飞跃俊杰

iOS标题偏移navigationItem,titleLabel,标题偏移到中间开始了,因为要插入图片,所以没有直接用title这个属性
代码如下:

        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(-30, 0, 300, 44)];
       
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(-30, 0, 300, 44)];

        [view addSubview:_titleLabel];
        self.navigationItem.titleView = view;

修改成这样就好了

        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(-30, 0, 300, 44)];
       
        _titleLabel = UILabel.new;

        [view addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.mas_equalTo(0);
        }];
        self.navigationItem.titleView = view;

注意:title最好不要用frame不久

上一篇下一篇

猜你喜欢

热点阅读