0

Im using the following codes to customize the navigationbar with an UIView. But why there is a gap in the left size of the navigationbar? ps. the gradient image is test.png.

Thanks in advance!

enter image description here

- (void)viewDidLoad
{
[super viewDidLoad];

// show image
UIImage *image = [UIImage imageNamed: @"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[imageView setContentMode:UIViewContentModeScaleToFill];
imageView.frame = CGRectMake(0, 0, 320, 44);

// label
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 0, 100, 44)];
tmpTitleLabel.text = @"title";
tmpTitleLabel.textAlignment = UITextAlignmentCenter;
tmpTitleLabel.backgroundColor = [UIColor clearColor];
tmpTitleLabel.textColor = [UIColor whiteColor];

CGRect applicationFrame = CGRectMake(0, 0, 320, 44);
UIView * newView = [[UIView alloc] initWithFrame:applicationFrame];
[newView addSubview:imageView];
[newView addSubview:tmpTitleLabel];
self.navigationItem.titleView = newView;
}

Edit 1

Here is my test code and screen shot

enter image description here

- (void)viewDidLoad
{    

[super viewDidLoad];

UIView * viewGreen = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIView * viewRed = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

[viewGreen setBackgroundColor:[UIColor greenColor]];
[viewRed setBackgroundColor:[UIColor redColor]];
self.navigationItem.titleView = viewRed;

[self.view addSubview:viewGreen];
}
2
  • @Abizern Just CGRectMake(0, 0, 320, 44), which is the frame of the navigationbar
    – lu yuan
    Aug 24, 2012 at 10:34
  • @Abizern plz check the update of my question:)
    – lu yuan
    Aug 25, 2012 at 2:12

2 Answers 2

2

your code is work fine.just check the image and transparency.

To add the line at last and check again

[self.view addSubview:newView];
0
1

problem is solved by the following code

[self.navigationBar insertSubview:imageView atIndex:1];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.