【问题标题】:rounded navigation bar image iphone sdk圆形导航栏图像 iphone sdk
【发布时间】:2015-07-11 01:56:27
【问题描述】:

我正在尝试将导航栏的titleView 设置为圆形图像。类似于您通常在消息传递应用程序中看到的个人资料图像。

我相信我应该能够通过执行以下操作来创建一个缩小的圆形图像:

UIImageView* profileImageView = [[UIImageView alloc] initWithImage:logoImage];
[profileImageView setFrame:CGRectMake(0, 0, 30, 30)];

//profileImageView.contentMode = UIViewContentModeScaleAspectFit;
// XXX contentMode commented out because enabling it causes the rounded corners to have no effect?
profileImageView.layer.cornerRadius = 15;
profileImageView.layer.masksToBounds = YES;
profileImageView.clipsToBounds = YES;
self.navigationItem.titleView = profileImageView;

这似乎是在我加载模拟器时创建我想要的图像,但图像出现在屏幕的左上角,然后立即捕捉到导航栏的中心。一旦到达导航栏的中心,它会重新缩放以占据导航栏的整个空间,而不是保留为一个小圆圈。我错过了什么?似乎我必须禁用导致我的图像放大以填充整个导航栏的机制。

【问题讨论】:

    标签: ios objective-c iphone ios7


    【解决方案1】:

    只需将其添加到包含视图中

     UIView * containView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    UIImageView* profileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.jpeg"]];
    [profileImageView setFrame:CGRectMake(0, 0, 30, 30)];
    profileImageView.layer.cornerRadius = 15;
    profileImageView.layer.masksToBounds = YES;
    profileImageView.clipsToBounds = YES;
    [containView addSubview:profileImageView];
    self.navigationItem.titleView = containView;
    

    截图

    【讨论】:

    • 嘿,这很好用,但是如何为圆形图像添加白色边框?
    • profileImageView.layer.borderWidth = 1.0F; profileImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    相关资源
    最近更新 更多