本来想在左上角显示头像,大小是36X36,一开始代码如下
UIButton *headButton = [UIButton buttonWithFrame:CGRectMake(0, 0, 36, 36)];
headButton.layer.cornerRadius = 36/2;
headButton.clipsToBounds = YES;
[ headButton sd_setImageWithURL:[NSURL URLWithString:url]
forState:UIControlStateNormal
placeholderImage:nil
options:SDWebImageAllowInvalidSSLCertificates
completed:nil];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:headButton];
self.navigationItem.leftBarButtonItem = item;
然鹅,头像被拉的很长,因为图片比较大。
解决方法是用一个UIView包含button:
UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 36, 36)];
[holderView addSubview:headButton];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:holderView];
self.navigationItem.leftBarButtonItem = item;