[英]Keep aspect ratio of an UIImageView in a custom UITableViewCell
[英]Custom UIBarButtonItem keep Aspect Ratio for Image but Space out Buttons
我將一些自定義圖像UIBarButtonItems添加到導航欄。 如果我不搞亂item.width屬性,則我的按鈕太靠近,但是如果我用width屬性將它們隔開,則按鈕圖像會變形。 如何保持按鈕圖像的長寬比,但又保留自定義項目的空間?
// Create custom map button
UIButton *mapButton = [UIButton buttonWithType:UIButtonTypeCustom];
[mapButton setImage:[UIImage imageNamed:@"map.png"] forState:UIControlStateNormal];
[mapButton addTarget:self action:@selector(dismissMapView:) forControlEvents:UIControlEventTouchUpInside];
[mapButton setFrame:CGRectMake(280, 25, 30, 30)];
UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithCustomView:mapButton];
button1.width = (self.drawer.frame.size.width / 5.0f); // Space it out a bit
// Create custom list button
UIButton *listButton = [UIButton buttonWithType:UIButtonTypeCustom];
[listButton setImage:[UIImage imageNamed:@"list.png"] forState:UIControlStateNormal];
[listButton addTarget:self action:@selector(scrollToRow:) forControlEvents:UIControlEventTouchUpInside];
[listButton setFrame:CGRectMake(280, 25, 30, 30)];
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithCustomView:listButton];
button2.width = (self.drawer.frame.size.width / 5.0f); // Space it out a bit
// Add buttons to drawer
self.drawer.items = @[button1,button2];
嘗試使用此代碼在navigationbar
添加自定義欄按鈕。
UIImage* image3 = [UIImage imageNamed:@"compose_message_button.png"];
CGRect frameimg = CGRectMake(250, 9, 43,32);
UIButton *SettingButton = [[UIButton alloc] initWithFrame:frameimg];
[SettingButton setBackgroundImage:image3 forState:UIControlStateNormal];
[SettingButton addTarget:self action:@selector(BtnWriteMessage:)
forControlEvents:UIControlEventTouchUpInside];
[SettingButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:SettingButton];
self.navigationItem.rightBarButtonItem=mailbutton;
[SettingButton release];
我希望這段代碼對您有用。
您可以使用UIBarButtonSystemItemFixedSpace
創建一些顯式的間隔項。
或者,您可以創建帶有自定義視圖的bar按鈕( initWithCustomView:
,然后可以根據需要布置內容視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.