I'm asking since the usual answer, modifying the frame in viewDidLayoutSubviews, does not work - unless you can find a mistake in my code. The frame gets set to the correct width and height, but iOS 7 does not respect the frame.
Currently, the app released long ago looks like this and works on iOS 6 and 7:
https://itunes.apple.com/se/app/eksjo/id435475192?mt=8
Recompiling gives this:
https://www.dropbox.com/s/pzyv2vhtlmlxkoe/Photo%202013-12-11%2009%2047%2030.png
-(void)viewWillAppear:(BOOL)animated {
UIImageView *iv=[[UIImageView alloc] initWithFrame:r(320-102/2,0,102,44)];
iv.image=[UIImage imageNamed:#"Eksjologo5bar.png"];
self.navigationItem.titleView=iv;
[iv release];
}
-(void)viewDidLayoutSubviews {
CGRect frame=self.navigationItem.titleView.frame;
frame.size.width=102;
frame.size.height=44;
self.navigationItem.titleView.frame=frame;
}
All I want to do is put a logo image in the center of the Navigation Bar. I'm looking for a minimum code change to the viewWillAppear code to do this and still be compatible with iOS 6.x.
Edit: It may also be an iOS 6 issue and not an iOS 7 issue. If you can explain why it should be done like in this question, it's an answer to my question: My UINavigationitem's TitleView getting expanded in ios 6
Here is what I do
UIImageView *logoImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, desired_image_width, desired_image_height)];
// if you need to resize the image to fit the UIImageView frame
logoImage.contentMode = UIViewContentModeScaleAspectFit;
// no extension name needed for image_name
[logoImage setImage:[UIImage imageNamed:#"image_name"]];
UIView *logoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, logoImage.frame.size.width, logoImage.frame.size.height)];
[logoView addSubview:logoImage];
self.navigationItem.titleView = logoView;
You may notice that I add the UIImageView instance to an UIView instance before setting the navigationItem's titleView. You may set the UIImageView instance to navigationItem's titleView directly, but the logo will be off center when you navigate to the next page and you still want to show the logo with the back button. The navigation bar will automatically put the UIView in the center, but UIImageView (although UIImageView is a subclass of UIView, I just don't know why).
Related
I'm trying to create a custom UIProgressView and I want it to look like this:
But everything that I'm trying is not working (iOS7). I want it to support iOS5+ and also in iOS7. Finally I did something and I almost there, but I have few problems.
This is how it looks now:
How can I put the image behind the progress view it self, also, how can I change the color of the progress bar it self and how can I force it to stay on the margins of the image?
How can I change the frame of the progress view?
This is my code:
- (void)createProgressView
{
// NOT CHANGING THE FRAME
self.packageUtilizingCellProgressView.frame = CGRectMake(105, 20, 91, 14);
self.packageUtilizingCellProgressView.progressViewStyle = UIProgressViewStyleBar;
// CHANGING THE FRAME
[[UIProgressView appearance] setFrame:CGRectMake(105, 20, 91, 14)];
}
I've also created a subclass of UIProgressView called MYProgressView and in drawRect I've added those lines:
- (void)drawRect:(CGRect)rect
{
UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"progress_view_transparent_background.png"]];
[background.image drawInRect:rect];
[self bringSubviewToFront:background];
}
Please tell me what am I doing wrong and how can I do it the right way?
Thanks in advance!
A solution could be to use a custom Progress View using ImageProgressBar.
Create your custom progress bar and add a UILabel on top of it.
It support iOS5+ and iOS7
I want to put my table view background below the nav bar. When I try to change frame nothing happens..
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, -100, 320, 480)];
imageView.image = self.theme.bgImage;
[self.tableView setBackgroundView:imageView];
In the docs it says:
A table view’s background view is automatically resized to match the
size of the table view.
So I think it's alway resized again by the framework.
Add UIImageview as subview to view and set appropriate image.Place imageview behind tableview. Make tableview background nil. Then try to change frame. This may help you.
I have a UIView that I have dropped inside of another UIView in IB (iPad version), I am doing this so I can control the background color of the region.
Odd thing is that if I set the background color of the UIView to GroupTableViewBackgroundColor, either in IB or code, the color is always white, it does not respect the color change, nor is it reflected in IB.
Has anyone seen this behavior and found a fix?
Thanks in advance.
I think I know what you are saying. You want the view to be the same as a group table view even though it isn't a group table view. This is what I did:
UITableView *tv = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
UIView *backgroundView = tv.backgroundView;
[self.view addSubview:backgroundView];
[tv release];
CGRect backgroundFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[backgroundView setFrame:backgroundFrame];
[self.view sendSubviewToBack:backgroundView];
I am not sure if it is related to your problem but Apple introduced some changes to UITableView for iOS 3.2. They introduced a new property to UITableView defined as follows:
#property (nonatomic, readwrite, retain) UIView *backgroundView;
This new view sits behind the table cells, header and footer which can be confusing if you set the table view background color. As a quick workaround, if you just want to change the color, you can remove the new view:
tableView.backgroundView = nil;
You should then get the same behavior as on the iPhone when you set tableView.backgroundColor. (Of course this may all change with iOS 4.2).
I have an issue with a UINavigationBar and its y-offset. The bar is displayed without a UINavigationController as superview, yet that should not matter. In the viewController where the navigation bar appears the setup looks like this:
// Add Basic View
CGRect viewFrame = [[UIScreen mainScreen] applicationFrame];
UIView *myView = [[UIView alloc] initWithFrame:viewFrame];
myView.backgroundColor = [UIColor greenColor];
self.view = myView;
[myView release];
UINavigationBar *myBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
.... add some Stuff to the bar...
[self.view addSubview:myBar];
[myBar release];
As I add the navigationBar as a chield View to self.view I assumed that origin.y = 0 would mean that the bar should get directly displayed below the status bar. This works as expected if I start the app on my iPad, rotate it once (or more) and then drill down to the view that is described above. In this case the UINavigationBar is displayed properly. Yet if I start my app and directly drill down to the controller described above (without rotating the device before this particular controller appears) the navigation bar slides 20 points below the status bar. But as soon as I rotate the device then, the bar is fine again. I have checked the viewFrame.origin.y value and it is 20 points in both situations, hence I do not understand why in one case the bar just seems to ignore the origin.y value of its superview but does not in the other.
I am really confused about this, has anybody else ever experienced such an issue?
Thanks a lot for your help!
Ps. I have also tried it with a UIToolbar, the problem is the same.
Yes. My solution is to set the "Full screen on launch" flag to on in Interface Builder for the window in the MainWindow-iPad.xib file. Then design your views as if the 20 pixel status bar were always displayed, so in my root view, I have a toolbar that is positioned 20px below the top of the screen in the content view.
UIImageView *navBarImageView = [[UIImageView alloc] initWithImage:navBarImage];
[navBarImageView setFrame:CGRectMake(0, 0, 320, 44)];
self.navigationItem.titleView = navBarImageView;
[navBarImageView release];
I am trying to add an image to my navigationBar, but when I used the code as listed above, it places the image into the navigationBar but leaves a grey gap to the left and right of the image. The image was created at 320x44, I'm not sure why it is resizing it despite the fact that I am setting the frame.
That's... probably not how you want to do that—it's not what the titleView is for. The usual way to do a custom navigation-bar background is to create a category on UINavigationBar, like so:
#implementation UINavigationBar(MyCustomBackground)
- (void)drawRect:(CGRect)r
{
[[UIImage imageNamed:#"my-navigation-background.png"] drawInRect:self.bounds];
}
#end
Throw that in a .m file in your project and you should see all of your navigation bars use "my-navigation-background.png" as their background.