将图像定位在导航栏IOS7上

时间:2014-07-30 07:49:39

标签: ios objective-c

我在导航栏中定位图像有几个问题(Xcode 5.1.1 IOS7)。

1)图像似乎无法移动到屏幕的左边缘。它和边缘之间总是有少量的空白区域。

2)当我旋转屏幕时,导航栏比iPhone模拟器“垂直”时更短。

这是viewDidLoad的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    /*
        Put image in nav bar
     */

    UIImageView * myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TheLogo45p.png"]];
    //[myImageView setFrame:CGRectMake(0,0,100,100)];
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:myImageView];
    self.navigationItem.leftBarButtonItem = item;
}

我希望能够在没有代码的情况下定位图像,但在可视化编辑器中这似乎是不可能的。我的代码中我做错了什么?

编辑:

新代码:.h文件

#import <UIKit/UIKit.h>

@interface AACLoginViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIView *btnLogin;

@property (weak, nonatomic) IBOutlet UIBarButtonItem *customItem;

@end

新代码:.m文件

@synthesize customItem;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    /*
        Put image in nav bar
     */


    customItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"TheLogo45p.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(backBtnAction)];

    customItem.imageInsets = UIEdgeInsetsMake(0.0, -10, 0, 0);// Change as you wish
    //[customItem setBackgroundVerticalPositionAdjustment:+3.0f forBarMetrics:UIBarMetricsDefault];
}

1 个答案:

答案 0 :(得分:1)

您可以使用Image Inset进行更改:

<强> 故事板:

enter image description here

<强> 编码:

    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navIcon.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(backBtnAction)];

    item.imageInsets = UIEdgeInsetsMake(0.0, -10, 0, 0);// Change as you wish
    //[customItem setBackgroundVerticalPositionAdjustment:+3.0f forBarMetrics:UIBarMetricsDefault];
    [self.navigationItem setHidesBackButton:YES];
    [self.navigationItem setLeftBarButtonItem: item];