【发布时间】:2014-08-19 06:52:01
【问题描述】:
我是 Xcode 和 Objective-c 的新手,我尝试制作一个应用程序,其导航菜单可以显示保存在 MySQL 数据库中的所有者的个人资料图片。我使用导航栏按钮,但似乎不能使用 UIViewImage 作为按钮。这是我的代码:
//Resize image
UIImage *miniProfile=[UIImage imageNamed:@"profile_icon.png"];//this is the hardcoded version
UIImage *tempImage = nil;
CGSize targetSize = CGSizeMake(25,25);
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectMake(0, 0, 0, 0);
thumbnailRect.origin = CGPointMake(0.0,0.0);
thumbnailRect.size.width = targetSize.width;
thumbnailRect.size.height = targetSize.height;
[miniProfile drawInRect:thumbnailRect];
tempImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Automatically change image to circle view
UIImageView *roundProfile = [[UIImageView alloc] initWithImage:tempImage];
roundProfile.backgroundColor = [UIColor clearColor];
roundProfile.layer.cornerRadius = miniProfile.size.width/2;
roundProfile.layer.masksToBounds = YES;
[self.view addSubview: roundProfile];
//Add image to navigation bar. However button cannot be clicked
UIBarButtonItem * profileImage = [[UIBarButtonItem alloc] initWithCustomView:roundProfile];
//Create two button on right navigation bar. One for image the other for real button
NSArray *profilebarButton = @[profileImage];
self.navigationItem.rightBarButtonItems = profilebarButton;
我可以将 UIImageView 放在导航栏中,但我遇到了 2 个问题: 1.) 如果图像大于 30x30,则图像不会出现,意味着调整大小不起作用。 2.) 我不能点击图片,需要创建另一个按钮来处理这个
【问题讨论】:
-
有很多这样的问题和答案。看看这个请stackoverflow.com/questions/11019914/…
-
非常感谢您的帮助。这段代码绝对是我搜索的内容^^
标签: objective-c button navigation uiimageview