【问题标题】:Custom UIBarButtonItem keep Aspect Ratio for Image but Space out Buttons自定义 UIBarButtonItem 保持图像的纵横比但间隔按钮
【发布时间】:2014-05-17 02:26:50
【问题描述】:

我正在向我的导航栏添加一些自定义图像 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];

【问题讨论】:

  • 设置 UIButton ImageEdgeInset .

标签: ios iphone objective-c


【解决方案1】:

尝试使用此代码在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];

我希望这段代码对你有用。

【讨论】:

    【解决方案2】:

    您可以使用UIBarButtonSystemItemFixedSpace 创建一些明确的间隔项。

    或者您可以使用自定义视图 (initWithCustomView:) 创建您的栏按钮,然后您可以随意布置内容视图。

    【讨论】:

      猜你喜欢
      • 2011-06-07
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 2017-04-11
      • 1970-01-01
      • 2013-07-07
      • 2017-02-13
      相关资源
      最近更新 更多