UIImageView not correctly updating bounds - ios

I am currently trying to program a game with a HP gauge represented by an UIImageView placed on a .xib file. I declared my IBOutlet as follows:
#property (weak, nonatomic) IBOutlet UIImageView *hpGaugeView;
and synthesized accordingly. It is also linked to the UIImageView instance in the xib.
In the xib, I set the length of hpGaugeView to 188 and height to 9.
Now this is what I did in viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[hpGaugeView setFrame:CGRectMake(49, 18, 123, 9)];
NSLog(#"%f, %f", [hpGaugeView frame].size.width, [hpGaugeView frame].size.height);
}
Although NSLog tells me the hpGaugeView's width is 123, when I run it the length appears unchanged compared to the one in xib. All the other setFrame: or setBound: worked fine in other viewControllers, so I'm just wondering what's happening here.
It is a small but crucial problem that is bugging me for a few hours straight...
Edit:
When I turn off AutoLayout, the resizing worked; it creates a bunch of other problems though...

With autolayout on, the subviews have not been laid during the viewDidLoad. The constraints are calculated a bit later and you can override the viewDidLayoutSubviews method to set your frame:
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
// Do any additional setup after subviews are laid.
[hpGaugeView setFrame:CGRectMake(49, 18, 123, 9)];
NSLog(#"%f, %f", [hpGaugeView frame].size.width, [hpGaugeView frame].size.height);
}
From the class reference:
viewDidLayoutSubviews
Notifies the view controller that its view just laid out its subviews.

I believe the problem is that you're not changing the frame of the UIImageView. The bounds refer to the position and size of the view in it's own coordinate system whereas the frame refers to the position and size in it's superview coordinate system.
If the clipsToBounds property is set yo YES, then nothing outside the frame of your view will be visible.

It turns out it is some weird weird interaction between the nib and the code...when I created the UIImageView programmatically it worked fine.

AutoLayout property worked only above IOS 6.0 only . Dragging of control to XIB instead of you can create the UIImageView through Code and add to current view. I have given the code for adding UIImageView programmatically.
Source Code:
// .h file
#property (weak, nonatomic) UIImageView *hpGaugeView;
//.m file
- (void)viewDidLoad
{
[super viewDidLoad];
hpGaugeView = [[UIImageView alloc] init];
[hpGaugeView setFrame:CGRectMake(49, 18, 123, 9)];
[self.view addSubview:hpGaugeView];
}
Initialize and add the hpGaugeView to subview inside the viewDidLoad function through programmatically.

Related

ImageView doesn't show in UIScrollView (objective C)

I'm currently trying to put images in a UIScrollView.
I checked a lot for an answer, but none of the topics in stackoverflow works in my code. I guess I just forgot a detail in it, but can't figure it out.
So, I have in a ViewController.m this code:
- (void)initTransportData
{
_transportsData = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 654, 414, 82)];
CGRect viewSize = _transportsData.bounds;
UIImageView *imgView = [[UIImageView alloc] initWithFrame:viewSize];
[imgView setImage:[UIImage imageNamed:#"image.png"]];
[_transportsData addSubview:imgView];
}
My _transportData is defined in my ViewController.h like that:
#property (strong, nonatomic) IBOutlet UIScrollView *transportsData;
When i test it, the scrollView display a black color (i set it in my main.storyboard).
I also have a MapView above my scrollView (don't know if it can help to understand).
PS: I call my intTransportData function below [super viewDidLoad] in the viewDidLoad function.
Does anyone know what is wrong?
Thanks
You have set frame like (0, 654, 414, 82) that means your y position is 654 of scrollview. Then it is below the screen height.
So , set frame properly.
You need to add that scrollview to main view (self.view).
If you have given outlet then no need to set frame again.
You should use autolayout to manage this things easily.
Your scenario should be like, UIScrollview - UIView - UIImageView and you need to set proper constraint to every views. If you unaware of auto layout then search tutorials on google and first learn it.
Hope this will help :)

UIImageView not redrawing

I'm using code to resize a UIImageView by reducing its width to .5 its original value.
The code is
- (void)viewDidLoad
{
[super viewDidLoad];
self.allisonHall.newWidth = .5;
self.allisonHall.frame = CGRectMake(self.allisonHall.frame.origin.x,
self.allisonHall.frame.origin.y,
self.allisonHall.frame.size.width*self.allisonHall.newWidth,
self.allisonHall.frame.size.height);
self.allisonHall.contentMode = UIViewContentModeLeft;
self.allisonHall.clipsToBounds = YES;
NSLog(#"%f",self.allisonHall.frame.size.width);
}
and the result of the NSLog is 140, .5*280 as expected.
However, in the view the UIImageView remains at its original width.
The UIImageView is linked as
#property (weak, nonatomic) IBOutlet XYZdiningHall *allisonHall;
within the view controller's .h file, where XYZdiningHall is a subclass of UIImageView
Should the UIImageView's frame be changing given this code or do I need to call another method to fire the change?
Do you need to use UIViewContentModeLeft? You can use UIViewContentModeScaleAspectFit or UIViewContentModeScaleAspectFill and the image will be redrawn (the aspect modes are related to the image view bounds, Left isn't).

Update the preferredMaxLayoutWidth for a multiline UILabel with unknown size (Auto Layout)

I have a custom view class which inherits from UIView. This class has an UILabel as its subview. In the init-function of this custom view class I set up everything needed like this:
//h-file
#import <UIKit/UIKit.h>
#interface MyCustomView : UIView
#property (strong, nonatomic) UILabel *myLabel;
#end
//m-file
#implementation MyCustomView
#synthesize myLabel = _myLabel;
- (id)init
{
self = [super init];
if (self) {
_myLabel = [UILabel new];
if(_textView){
_myLabel.highlightedTextColor = [UIColor whiteColor];
_myLabel.translatesAutoresizingMaskIntoConstraints = NO;
_myLabel.lineBreakMode = NSLineBreakByWordWrapping;
_myLabel.numberOfLines = 0;
_myLabel.backgroundColor = [UIColor clearColor];
[self addSubview:_myLabel];
}
}
return self;
}
#end
I also set up a bunch of constraints to manage padding inside my custom view - furthermore there are constraints to layout multiple MyCustomView-instances for both vertical and horizontal axis as well.
To get a multilined label output I have to set the preferredMaxLayoutWidth-property of the UILabel myLabel. The width depends on the free space available. At http://www.objc.io/issue-3/advanced-auto-layout-toolbox.html I read, that I can let Auto Layout calculate the width first and set it as preferredMaxLayoutWidth after the frame of the MyCustomView-instance (the label inside is single lined at this moment) has been set.
If I put the following function into the MyCustomView, the label still has a single line of text:
- (void)layoutSubviews
{
[super layoutSubviews];
float width = _myLabel.frame.size.width;
_myLabel.preferredMaxLayoutWidth = width;
[super layoutSubviews];
}
If I set the preferredMaxLayoutWidth to an explicit value inside the init-function, the label is multilined.
Does anybody know what I am doing wrong here?
Thanks in advance!
Without seeing all the constrains you have setup for your custom view, and the superview that contains it, it's really hard to determine the problem, I suggest you to print out all the view frames of the entire view hierarchy starting from the view controller's view at viewDidLayoutSubviews and determine if the label and its superviews have correct frame set.
I have an encountered similar issues with dynamic label size and scroll view so I created a prototype here, might be useful to you too: https://github.com/briandotnet/AutoLayoutScrollViewExperiment

UIScrollView not scrolling after changing from regular View

I have a xib with a View, I changed the View class to be UIScrollView.
Now I have set contentSize to some big number (2000, 2000).
Here is my code:
This allows talking to my scrollview without casting every time:
- (UIScrollView *)scrollView {
return (UIScrollView *)self.view;
}
This I do in viewDidLoad:
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 2600);
self.scrollView.scrollEnabled = YES;
I know viewDidLoad is getting called fine. But the scrollView is not reacting. I don't have any touch responders or anything that can absorb touches.
For this, first set delegate of scrollView inside your xib files then create an IBIoutlet for your scrollview. Remind that scrollView should connect with its refrencing outlet then Assign a propertyy as Strong to your scrollView.
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
and synthesize it.
#synthesize scrollView;
then try to set contentSize.
scrollView.delegate=self;
scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 2600);
scrollView.scrollEnabled = YES;
I hope it works for you.
I have found a problem, I was setting superview too small.

UIButton Changing Position

I have a button set up in IB. I have an IBOutlet set up and the onscreen object linked to it. Is there a way to programmatically change that buttons position and/or size? I know you can change the title and some things but I don't see how to change it's position or size.
Now I would like to change the position of it accordingly. Is it possible? If yes, please let me know how, since I am trying to change the position of my button in the following code, but it does not work in the header file.
#property (nonatomic, strong) IBOutlet UIButton *mybuttonOutlet;
In the implementation file:
-(void)viewDidLoad {
screenSizeHeight=[UIScreen mainScreen].bounds.size.height;
if(screenSizeHeight==568)
mybuttonOutlet.frame= CGRect(38, 464 ,157,25);
if(screenSizeHeight==480)
mybuttonOutlet.frame= CGRect(38, 364 ,157,25);
}
Remove Use Autolayout from the button in IB or storyboard.
If you want to adjust positions with Autolayout enabled, you will have to change your code like this
-(void)viewDidLayoutSubviews {
screenSizeHeight=[UIScreen mainScreen].bounds.size.height;
if(screenSizeHeight==568)
mybuttonOutlet.frame= CGRect(38, 464 ,157,25);
if(screenSizeHeight==480)
mybuttonOutlet.frame= CGRect(38, 364 ,157,25);
}
Basically you need to perform any custom layout adjustments in viewDidLayoutSubviews method if Autolayout is enabled
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
#import "ViewController.h"
#interface ViewController ()
#property(nonatomic, strong) IBOutlet UIButton *theButton;
// -(IBAction)moveTheButton:(id)sender;
#end
#implementation ViewController
#synthesize theButton;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)moveTheButton:(id)sender{
//set default position
CGRect btFrame = theButton.frame;
btFrame.origin.x = 145;
btFrame.origin.y = 285;
theButton.frame = btFrame;
//position changing
btFrame.origin.x += 40;
theButton.frame = btFrame;
//new size of button
CGRect rect=CGRectMake(145, 285, 190, 30);
[self.theButton setBounds:rect];
}
#end
I eventually went for the constraints. Get an outlet of the constraints that determine the position of the button (top, bottom, leading, trailing) and change the constraint(s) value(s).
self.constBtnSubmitTrailing.constraint = 50.0
This so you can keep AutoLayout enabled.
The solution I figured out for this is to create new View object inside the main View, and put my "dynamically changing objects" inside that View (as shown in picture).
Object hierarchy
Then I use Auto-Layout to position the new View in the main View.
But bugButton:UIButton which is inside the new View, changes position as expected with:
bugButton.frame.origin = CGPoint(x: newX, y: newY)
Please perform this check.
-(void)viewDidLoad{
if(self.mybuttonOutlet==nil)NSLog(#"Button is NIL");
}
Now if you get the log "Button is NIL", then probably you forgot to link your IB button to your variable mybuttonOutlet.

Resources