UIButton not displaying background image on device or simulator - ios

I really thought stuff like this wouldn't happen to me, but here it is:
I Setup some Buttons on the StoryBoard. When I Compile the App into a Device or the Simulator, one of the Buttons shows the Image and the Rest don't.
I Already Checked that the Images are assigned to the Target and that the Resources appear on the Project.
Any Ideas?

Well, this is Embarrassing.
Turns out I Was Modifying the superview.bounds Due to a Previous Interface Issues and i Forgot About them.
The Problematic Code:
// to Arrange the Layout:
- (void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
self.view.superview.bounds = CGRectMake(0, 0, 356, 421);
}
So I Removed This Line: self.view.superview.bounds = CGRectMake(0, 0, 356, 421); and Everything is now OK.
Lesson: DO NOT PLAY WITH THE BOUNDS OF YOUR SUPERVIEW IF YOU WANT AUTOLAYOUT TO WORK.
Thanks to #Maverick and #Fahim Parkar for pointing me on the Right Direction :-)

Related

UIButton imageEdgeInsets not updating at run time

So here is my code, it's fairly strait forward.
CGFloat spaceing = 5;
self.premiumButton.imageEdgeInsets = UIEdgeInsetsMake(spaceing,spaceing,spaceing,spaceing)
NSLog(#"immage - %#", NSStringFromUIEdgeInsets(self.premiumButton.imageEdgeInsets));
right should update the Image to be in the center of the button with a 5 point padding on all sides(I've removed the text for this particular state)
but the log comes back with
immage - {0, 0, 0, 0}
Infact I placed this in initWithCoder which is how this view is being built from a xib and got the same thing despite the fact that in the interface builder I set everything.
What am I missing everything else on stack overflow has code samples that look just like mine marked as the answer but it's not doing anything for me.
I have tried the UIButton setTitleEdgeInsets method but no go.
- (void)layoutSubviews update imageEdgeInsets

How to set the background image according to screen size?

I want to set an image according to screen size. I've been going through many codes on it but I've not found a working solution yet. I'm using Xcode 5.1.1 and Iphone Retina 4inch. Whenever I add my background image, it becomes huge and goes out of screen size. Kindly help me.
Use the code from Anbu's answer to get the screen height and set the image to aspectFit to avoid it from getting huge and out of bounds:
yourImageview.contentMode = UIViewContentModeScaleAspectFit;
yourImageview.clipsToBounds = YES;
If your view is directly added to a UIViewController you should use the viewWillLayoutSubviews (doc) method to do the resize. Something like this:
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
self.imageView.frame = self.bounds;
}

Strange appearance of the status bar after upgrading to ECSlidingViewController 2

It seems that one particular aspect of iOS programming is to diagnose these weird, seemingly trivial yet frustratingly obscure small problems.
So today, I was happily woking on my recent iOS project, and I decided to upgrade my project to the latest version ECSlidingViewController, what harm could it do right? Just update a few deprecated methods that's all.
So I did all of that. Everything works fine, beautiful. However, I noticed that the status bar is behaving strangely! It is not appearing when I display one of my underLeftViewController, and it is in a weird shade when I push segue that particular underLeftViewController into one of its subsequent VC. What?? How could this be happening? Anyway, a picture is worth a thousand words:
So here is it acting nice and normal:
Now it disappears!!!:
Now it has a weird shade!!!:
And here is a picture of the app with the sliding view controller slided out:
I must have done something crazy to my status bar somewhere, then I thought.
So I looked into my implementation file for the VC where statusbar is acting crazy. It is in fact a subclass of UINavigationController, and its viewDidLoad is empty except with the [super viewDidLoad] line. So nothing suspicious here.
The run test page is in fact the rootViewController for the navVC, so I looked into it. I put all of my view setup code in its viewDidLoad, and this is what it looks like:
- (void)viewDidLoad
{
[super viewDidLoad];
// remove advanced button
self.navigationItem.rightBarButtonItem = nil;
self.navigationController.view.layer.shadowOffset = CGSizeMake(1, 0);
// setupGaugeView
[self setupGauge];
// add run test button
[self setupRunTestButton];
// setup notification container
CGRect notificationContainerFrame;
if ([WRGlobalHelper currentDeviceVersion] >= 7) {
CGFloat statusBarHeight = [WRGlobalHelper statusBarHeight];
notificationContainerFrame = CGRectMake(0, [[self.navigationController navigationBar] bounds].size.height+statusBarHeight, self.view.bounds.size.width, 1);
for (UIView *subview in self.view.subviews) {
CGRect newFrame = subview.frame;
newFrame.origin.y += [WRGlobalHelper statusBarHeight];
subview.frame = newFrame;
}
} else {
notificationContainerFrame = CGRectMake(0, [[self.navigationController navigationBar] bounds].size.height, self.view.bounds.size.width, 1);
}
self.notificationContainerView = [[UIView alloc] initWithFrame:notificationContainerFrame];
self.notificationContainerView.clipsToBounds = NO;
self.notificationContainerView.layer.backgroundColor = [UIColor clearColor].CGColor;
[self.view addSubview:self.notificationContainerView];
// some other unrelated stuff omitted....
}
And the `viewDidLoad's for the VCs where the status bar is acting normal or bizarre but with shade is all quite plain as well, they look like
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view addGestureRecognizer:self.slidingViewController.panGesture];
}
Mind blown and I give up at this point. I've spent nearly 2 hours on this single issue already, and my brain hurts at the thought of the disappearing status bar. The almighty and omniscient SO, please help me! Thank you very much!
The status bar is not disappering, the text is just changing its color based on its assigned Style.
This answer will help

Dynamically reposition a UI Button

I'm trying to change the position of my UIButton, which was positioned and sized in interface builder. I'm stumped.
I tried this code at the end of viewDidAppear:
[_smartphoneButton setFrame:CGRectMake( 20, 20, 100, 50 )];
Nothing changes. And yet i know _smartphoneButton is valid, because _smartphoneButton.hidden = YES; works, as well as changing the UIButton image.
And suggestions?
PS, none of these work either:
_smartphoneButton.frame = CGRectMake( 20, 20, 100, 50 );
or
_smartphoneButton.center = CGPointMake(10.0f, 10.0f);
Thanks in advance!!!
Agree with CodaFi, put a break point to see the value of _smartphoneButton.
But even if viewWillAppear will work, I would suggest moving your code to viewDidLoad for the following reason.
viewWillAppear can get call several time in the live of a ViewController and you will be modifying the position of your button ofter for nothing.

iPhone Custom UISlider preload and rounded edges issues

I'm trying to implement a custom UISlider, I've extended it with a class called UISliderCustom which has the following code:
#implementation UISliderCustom
- (id)initWithCoder:(NSCoder *)aDecoder{
if(self == [super initWithCoder:aDecoder]){
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, 200, 13);
UIImage *slideMin = [[UIImage imageNamed:#"slideMinimum.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
UIImage *slideMax = [[UIImage imageNamed:#"slideMaximum.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
[self setThumbImage:[UIImage imageNamed:#"slideThumb.png"] forState:UIControlStateNormal];
[self setThumbImage:[UIImage imageNamed:#"slideThumb.png"] forState:UIControlStateHighlighted];
[self setMinimumTrackImage:slideMin forState:UIControlStateNormal];
[self setMaximumTrackImage:slideMax forState:UIControlStateNormal];
}
return self;
}
#end
I ran into two small problems
When I slide over the slider to one of the edges (progress = 0.0 / progress = 1.0), I can clearly see "left overs" in the sides, im not sure how to handle that as well, unfortunately :)
Slider images:
Problem:
I see the regular UISlider (blue and silver) for a couple of seconds, and only then the custom graphics is loaded, or when i actually move the slider. I'm not sure why this is happening.. EDIT: This only happens in the simulator, works fine now.
Thanks in advance for any assistance :)
Shai.
You have no need to subclass UISlider to achieve this effect, and if you did you certainly wouldn't set the track images in the drawRect method. drawRect should contain drawing code only, it is called whenever any part of the control needs redrawing.
Set the thumb and track images in a separate method, either within your subclass (called from initWithFrame and initWithCoder) or in the object that creates the slider in the first place. This only needs to be done once, when the slider is first created. Don't override drawRect.
You don't need to call awakeFromNib manually either, unless you have some specific code in there as well? That would be a common place to set custom images in a subclass, if you only ever used the slider from IB.
For the square ends, the problem is that the extreme edge of your track image is square, so it is showing around the thumb. Make both ends of the track image rounded, with a 1px stretchable area in the middle, like this:
I just had a very similar problem myself. It turned out that the size (width x height) of the slider that I added in interface builder didn't match the sizes of the images I was using to customize the slider. Once I made them match, those "leftovers" at the ends of the slider went away.

Resources