iOS unable to set element position programmatically - iphone

I am unable to set label and image position programmatically. I am trying to set is as,
navBarSocial.frame = CGRectMake(navBarSocial.frame.origin.x, navBarSocial.frame.origin.y, webView1.frame.size.width, navBarSocial.frame.size.height);
status.frame = CGRectMake(100, 50, status.frame.size.width, status.frame.size.height);
loadingImage.frame = CGRectMake(100, 50, loadingImage.frame.size.width, loadingImage.frame.size.height);
navBarSocial increase its witdth properly but label (status) and loadingImage remains at same position. I also set autosizing properties deactivated on xib but no success.

Not sure of your question. You speak of position, which makes sense they are staying in the same position as you have statically set them to x = 100 and y = 50. In terms of size, it seems you are setting the size to the current size of the item. In other words you are passing the current size in as the size you want to set the element you are trying to resize to.

loadingImage.frame = CGRectMake(100, 50, loadingImage.frame.size.width, loadingImage.frame.size.height);
loadingImage sets it's width to its width and its height to its height. What do you expect to change? You probably need to change the width and the height to something different that it already has..
Setting geometry should be done in viewWillAppear: and viewDidAppear methods. Also check your outlet connections.
Btw use only properties, not ivars.

Related

A few questions regarding UILabel and UIImageView placement handling?

I have two questions regarding the placement and handling UILabel's and UIImageView's:
a. I'd like to know if its possible to detect the location of the "edge" of the text in a UILabel, to put a UIImageView a certain distance to the left or right of the UILabel (with the same height), for example: (UILabel -certain distance in width- UIImageView). (Preferably a method that could be used to detect the edges of a UIImageView as well)
b. I'd also like to know how the creation of UILabel's and UIImageView's programmatically works, I understand that to create for example a UIImageView it's as simple as:
UIImageView *myImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
but I'd like to know: If I have a method to create a UIImageView programmatically, and it sets the CGRect based on a factor that changes, would I need a new name for every UIImageView I create in code, or could I use the same name and add them every time the method runs by calling addSubView? If I do need to individually name them all, and I'm unsure how many I will need to create (because it depends on the user), what is the best method to go about this? If I can create them all with the same name, will they all respond to the same name? Example, if I set myImgView to hidden, will this effect all the UIImageView's named myImgView?
You seem to be new to object oriented programming (as evidenced by the last part), and I would highly recommend you read this: Object-Oriented Programming with Objective-C
But I will answer your question about the UIViews. UIImageView and UILabel are both subclasses of the UIView class. UIView has a special kind of variable called a property. That means that the variable can be set on an instance of that class by another class and can then be accessed from other classes as well. UIView has the frame property that contains the outline of the view in a CGRect. When you have a bit of code like
ImageView *myImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
You are creating an instance of the UIImageView class, allocating memory for it, and initializing it with a frame (in this case you are using rect with zero height and width, which is a bit useless). In order to set the frame to something useful, you use the format CGRectMake(originX, originY, width, height). Keep in mind that on iOS, the origin is on the upper left hand corner of the view, and a higher y value will be farther down the screen. If you need to change the frame later, rather than creating a whole new instance, you can simply alter the property on the existing instance:
myImgView.frame = CGRectMake(10, 20, 50, 100);
//this will make the frame of myImgView to a rectangel 10 units off of the right side of the view, 20 units down from the top of the view, and have a width of 50 units and a height of 100 units
However, you have declared myImgView as a local variable, meaning that you can only access it in the function that you have declared it in, at the time that you have declared it. In order to access it anywhere and anytime, you will need to declare it as an Instance Variable, meaning that you declare it in the .h file.
#interface MyView : UIView {
UIImageView *myImgView;
}
#end
Then the variable will be accessible anywhere in that class. Altering its frame as I demonstrated above will alter the frame in the view.
Finally, you can get the frame of an view if you need to do calculations with it. For example, you can get the frame of myImgView like so:
CGRect viewFrame = myImgView.frame;
CGPoint origin = myImgView.frame.origin;
CGSize width = myImgView.frame.size;
float x = myImgView.frame.origin.x;
float height = myImgView.frame.size.height;
//I think you can get it from here
Now with UILabels, it can be a bit trickier. The frame for a UILabel does not necessarily match the frame that the text takes up. Usually the actual frame is a bit smaller. In order to get the frame for the text of a UILabel, use something like this:
//UILabel *myLabel has been declared either in the .h or earlier in the function
CGRect textFrame = CGRectMake(myLabel.x, myLabel.y, [myLabel.text sizeWithFont:myLabel.font].width, [myLabel.text sizeWithFont:myLabel.font].height);
Now, finally, to put it all together, this would be how to calculate a new frame for your UIImageView:
myImgView.frame = CGRectMake(myLabel.x + [myLabel.text sizeWithFont:myLabel.font].width, yOrigin, width , height);
//where myImgView is your UIImageView myLabel is your UILabel, yOrigin is the y value of the origin of the imageView, and width and height are the width and height of the image.
Well I hope that helps and I highly suggest you read the object oriented programming guide.

Best way for horizontal scrolling of image

what is the best way for horizontal scrolling of image in iphone sdk. Image has to be get from web server something like lazy loading?
please help me.
Here is my suggestion:
First, set fixed size for your UIScrollView.
scroll.size = ... // your size
Next, determine width and height for contentSize of your scroll. Fix the height. the width is the value changeable. After each lazy image is loaded successfully, adjust the width.
Assume, all the image have the same height. Then, each time when there's an image is loaded, the code is:
(in the below code, I assume that the new image is appended. In your case, you can configure properly for your need)
float oldScrollWith = scroll.contentSize.width
// creat new_image_view (UIImageView)
// configure it to display in the scroll
...
new_image_view.frame = CGRectMake(oldScrollWidth, 0, imageViewWidth, fixed_image_height)
// append the new view to the scroll
float newScrollWidth = oldScrollWidth + new_image_view.frame.size.width // re-calculate the width for the contentSize of scroll
scroll.contentSize = CGSizeMake(newScrollWidth, fixed_image_height);
[scroll addSubview:new_image_view];

Is is possible to have a XIB then set the size and have everything scale to fit in a UIView?

I'm new to iOS development and I'm not sure if what I want to do is possible.
Basically I'm retrieving some products via a webservice and I want to display each one across the screen, 4 squares per row. Each one of these squares (with image and name of product) is a UIView which has a .h,.m and xib file. I create each view like this:
CategoryItemView *catItem = [[[NSBundle mainBundle] loadNibNamed:#"CategoryItemView" owner:self options:nil] objectAtIndex:0];
[categoryItems addObject:catItem];
and I position it like:
while (colCount < cols && items < [categoryItems count]) {
CGRect viewRect = CGRectMake( x * colCount + pad , (row * (x-pad)) + pad*row + pad, x - pad, x - pad );
CategoryItemView* myView = [categoryItems objectAtIndex:items];
myView.frame = viewRect;
[self.view addSubview:myView];
colCount++;
items++;
}
I want to use a xib so I can layout all the elements. However I cannot find a way to lay out the xib so that when positioning the UIView like this all the elements are scaled to fit and keep their relative positions in the UIView. Is this possible?
Update: You can view a 2 class example here thats not working http://home.glasscubes.com/share/s/d57sb19
thanks
autoresizingMask is the property you are looking for on those views.
From the docs:
autoresizingMask
An integer bit mask that determines how the receiver resizes itself
when its superview’s bounds change.
#property(nonatomic) UIViewAutoresizing autoresizingMask
Discussion
When a view’s bounds change, that view automatically resizes its
subviews according to each subview’s autoresizing mask. You specify
the value of this mask by combining the constants described in
UIViewAutoresizing using the C bitwise OR operator. Combining these
constants lets you specify which dimensions of the view should grow or
shrink relative to the superview. The default value of this property
is UIViewAutoresizingNone, which indicates that the view should not be
resized at all.
When more than one option along the same axis is set, the default
behavior is to distribute the size difference proportionally among the
flexible portions. The larger the flexible portion, relative to the
other flexible portions, the more it is likely to grow. For example,
suppose this property includes the UIViewAutoresizingFlexibleWidth and
UIViewAutoresizingFlexibleRightMargin constants but does not include
the UIViewAutoresizingFlexibleLeftMargin constant, thus indicating
that the width of the view’s left margin is fixed but that the view’s
width and right margin may change. Thus, the view appears anchored to
the left side of its superview while both the view width and the gap
to the right of the view increase.
If the autoresizing behaviors do not offer the precise layout that you
need for your views, you can use a custom container view and override
its layoutSubviews method to position your subviews more precisely.

How can i fix my scrollView dynamically when i have more than one textViews in it?

I have a scrollView which consists of 3 textViews, buttons and labels in a detailView. i am using 3 text view because i need different fonts for my view title, date and description. problem is sometimes description is long and sometimes its small and same thing with headings. then view doesn't loog good at all because of alot of empty spaces between title and description. Is it possible to set the size of textView and scroll view dynamically or is there any better approach to solve this problem. thanx in advance
You need to adjust the frames of the text views to match their respective contentSizes (see the top answer here: How do I size a UITextView to its content? on how to do that, uses the contentSize property of the textview), then you adjust the contentSize of the scrollView based on the frames of all the controls.
Assuming your textviews are placed consecutively vertically (just adjust the code if there is spacing, etc.):
// (first adjust each text view's frame per the linked answer)
...
// then adjust the frames of the content
CGRect topTextViewFrame = topTextView.frame;
CGRect middleTextViewFrame = middleTextView.frame;
middleTextViewFrame.origin.y = topTextViewFrame.origin.y + topTextViewFrame.size.height;
middleTextView.frame = middleTextViewFrame;
CGRect bottomTextViewFrame = bottomTextView.frame;
bottomTextViewFrame.origin.y = middleTextViewFrame.origin.y + middleTextViewFrame.size.height;
// then adjust your other controls based on these frames, for example:
CGRect myButtonFrame = myButton.frame;
myButtonFrame.origin.y = bottomTextViewFrame.origin.y + bottomTextViewFrame.size.height;
// finally adjust the contentSize of the scrollview assuming the button is the bottom element
CGSize csize = myScrollView.contentSize;
csize.height = myButtonFrame.origin.y + myButtonFrame.size.height;
myScrollView.contentSize = csize;
You could set the size of the frame to be dependent on the character length by setting the frame at the onset to be:
CGRectMake (0.0, 0.0, [yourString count]*10, 30.0);
This is what I did when I had a UIPopover come up with a variable name.

Custom UIProgressView

I'm using a UIProgressView in my application, which is working great right now. But I want to make it larger (height-wise) and color it a different color.
I'm looking to use PDColoredProgressView for the color, and alter the -(void)drawRect:(CGRect)rect method to change the height, however I can't decide where I would actually alter the height. Any suggestions on what to change?
So it turns out you can resize it like any other view.
[coloredProgressView setFrame:CGRectMake(0, 0, 300, 25)];
Setting the frame side didn't seem to work for me. Setting the transform to a CGAffineTransformMakeScale() can scale it up - not sure if that causes any other problems though.
To change the height of progressView try below code: ( works with Swift 5 )

progressView.transform = CGAffineTransform(scaleX: 1, y: 4) // y present the wanted height
and 1 present the current width, so if you change it to 3 then it will mean current width x 2