我必须创建一个UIBarButton,它上面有一个地球仪和一个文本“Map”。
我做了什么:
结果:
现在我得到的是一个平面按钮,其上的地球图像被拉伸,与其中的文本重叠。
我已经搜索过很多但我只找到了显示背景图片和按钮上方文字的链接。没有像图像和文字一样。
任何人都可以按照attachment中显示的方式帮助我创建它。
答案 0 :(得分:2)
试试这个:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"Title" forState:UIControlStateNormal];
UIImage *butImage_on = [[UIImage imageNamed:@"closebutton_on.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
UIImage *butImage_over = [[UIImage imageNamed:@"closebutton_over.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
[button setBackgroundImage:butImage_on forState:UIControlStateNormal];
[button setBackgroundImage:butImage_over forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:@"globe.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(closeProfilePage) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(0, 0, 61, 30);
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 2)];
[button setImageEdgeInsets:UIEdgeInsetsMake(30,
20,
0,
20)];
UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
self.navigationItem.leftBarButtonItem = backButton;
注意:您需要根据您的要求管理插入内容。
答案 1 :(得分:0)
将图像与@ 2x一起使用。如果您的图形是高分辨率。根据640 X 960 px;: -
UIView *parentView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 6,51, 44)];
UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 6, 51, 30)];
[btnBack setBackgroundImage:[UIImage imageNamed:@"back@2x.png"] forState:UIControlStateNormal];
[btnBack addTarget:self action:@selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[parentView1 addSubview:btnBack];
[btnBack release];
UIBarButtonItem *homeBarButtomItem2 = [[UIBarButtonItem alloc] initWithCustomView:parentView1];
[parentView1 release];
[self.navigationItem setLeftBarButtonItem:homeBarButtomItem2];
[homeBarButtomItem2 release];