【问题标题】:Positioning Navigation Bar buttons in iOS 7 [duplicate]在iOS 7中定位导航栏按钮[重复]
【发布时间】:2013-10-18 00:49:52
【问题描述】:

我正在为导航栏按钮使用自定义图像。在 iOS 6 中,如果我们将左侧栏按钮项设置为按钮,其 x 值从 10px 开始。但是在 iOS 7 中,如果我们做同样的事情,按钮的 x 值从 20px 开始。有什么方法可以让它从 10px 开始,因为按钮的外观在 iOS 7 中不是很好?

【问题讨论】:

    标签: iphone ios ipad ios6 ios7


    【解决方案1】:

    UIBarButtonItems 可以使用 initWithCustomView: 方法进行初始化。因此,您可以创建一些自定义视图(在您的情况下,导航栏按钮项带有自定义图像)并使用该自定义视图初始化栏按钮项。示例:

        CustomView *view = [[CustomView alloc] initWithImage:[UIImage imageNamed:@"back.png"]];
        UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithCustomView:view];
    

    你可以在 initWithImage: CustomView 的方法中设置任何你想要的框架:

    - (id)initWithImage:(UIImage *)image {
        self = [super initWithFrame:CGRectMake(0, 0, 50, 44)];
    
        CGRect backArrowFrame;
    
    
        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
            backArrowFrame = CGRectMake(-8, 12, 12.5, 20.5);
        } else {
            backArrowFrame = CGRectMake(-2, 12, 12.5, 20.5);
        }
    
        UIButton *imView = [[UIButton alloc] initWithFrame:backArrowFrame];
        [imView setContentMode:UIViewContentModeScaleAspectFit];
        [imView setBackgroundImage:image forState:UIControlStateNormal];
        [imView addTarget:target action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    
        [self addSubview:imView];
    
    
        return self;
    }
    

    这样可以改变UIBarButtonItem的框架。

    【讨论】:

    • 我知道这一点..我不想走这条路。我期待像 toplayoutguide 这样的东西,我们可以用它来为这些按钮提供指导。
    【解决方案2】:

    不,您不能更改 UIBarButtonItem 框架。相反,子类UINavigationBar

    【讨论】:

      【解决方案3】:

      在ios7中添加按钮作为导航项如下

      UIButton *btnAdd = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
      
      [btnAdd setContentMode:UIViewContentModeScaleAspectFit];
      
      [btnAdd setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
      
      [btnAdd addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
      
      UIBarButtonItem *btnAdd = [[UIBarButtonItem alloc] initWithCustomView:imView];
      
      self.navigationItem.rightBarButtonItem = btnAdd;
      

      【讨论】:

        猜你喜欢
        • 2011-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多