Objective-C Auto Adjust Subviews - iphone

I'm looking to write a method for an iPhone app that will auto adjust a UIViews's subviews Y values depending on the available space within that view. I'll try my best to explain without getting too confusing.
I have a Container view that is housing up to 3 Subviews. With in each subview is a button that removes the sub view from the container view, when the subview is removed i would like to adjust the remaining subviews to take up whatever space that has been opened by the removed subview.
Thanks in advance for any help!

One way is to make your wrapper view a custom UIView subclass. In that subclass, maintain a separate NSMutableArray of your subviews wherein each subview's array index corresponds to its position on the screen.
With this in place, you have a couple of options. One is to overide didAddSubview:. Based on the frame of the subview being added, you can determine the subview's position and insert reference to the subview at the appropriate index in your array.
A cleaner option is to implement your own custom method like this:
- (void)insertSubview:(UIView *)subview atPosition:(NSInteger)position;
where position is an index identifying which "slot" the subview should fill. You can set the frame for the subview explicitly within this method, along with any other subviews that are impacted by the insertion. Then insert the subview into your array at the corresponding index.
Finally, override willRemoveSubview:. In it you can use indexOfObject: on your array to find the position of the subview being removed. Then simply adjust the frames of all the subviews that follow it, and remove the subview from your array.
(One other alternative is to skip the array and just use the tag property to indicate the position of each subview. I don't like this option though. Using the tag property for anything always feels like a terrible hack to me.)

Well the first thing you need to do is signal your container that a subview has been removed.
Then you can take the height value of the view that was removed, divide it by the number of remaining subviews, and then expand the height of those remaining subviews.
Then you set your first remaining subview to Y coordinate 0, and the second (if there is one), to a Y coordinate of the first subviews height value

Related

Setting alpha a group of elements in a view in SWIFT

I've created a layout using the Xcode's Autolayout. I have a number of views with label and imageView inside. Is there any way to change the alpha for labels and imageView without creating outlets for all of them in ViewController in code?
Could I change the alpha for all the elements inside the view in code, not changing it for the view?
You can enumerate through a views subviews and set the alpha that way.
for v in view.subviews {
let view = v as! UIView
view.alpha = 0
}
Another approach is to use an IBOutletCollection which you can create in interface builder
Sure, there's lots of ways to handle this.
You could set up an IBOutletCollection of views who's alpha you want to change. An IBOutletCollection is really just an array of view objects that the runtime sets up for you. Beware that the order of the objects in the collection view is not guaranteed.
You could set a numeric range of tags on your views (starting with a non-zero value) and then use a for loop and the UIView method viewWithTag.
You could loop through all of a view's subviews in code and manipulate the view's alpha directly.

UIViews, subviews and alpha

I have three views on top of each other inside a superview.
superview
- subview 1 (CD Case)
- subview 2 (Artwork)
- subview 3 (Shine)
when I animate the alpha property of the superview it seams to affect the alpha of all subviews i.e. while the animation is running subview 1 the CD Case shines through the artwork.
While blending all three into one UIImage solves this problem, but for performance reasons this not really an option. (The views are display in a grid view and loaded on demand.)
Is there any other way to avoid this behaviour?
You can either reverse the fade, as suggested by #debleek63. Or you can change the shouldRasterize flag on the superviews layer.
myView.layer.shouldRasterize = YES
This will make it use 'group opacity' and everything should composite as you'd expect. The first way is probably more performant, but not as 'correct'.

scaling UIScrollView contentSize to fit UIView with variable-height UITableView inside

I have a uiview with a uiscrollview as a subview in a nib.
Then, I load another uiview with a uitableview as a subview of this other uiview (among other elements inside this view) as a subview of the uiscrollview.
That is, UISCrollView -> UIView -> UITableView (and some other views inside the UIView).
And I need to do the following:
Once I know the final height of the tableview (depending on the number of cells and height of them) I need to resize the uiview containing it and, in turn, resize the contentSize property of the containing scrollview.
But I don't know at which point the final size of the tableview is known (because it can dynamically change size depending on the amount of text its cells will hold) and neither do I know by how much will the tableview exceed the uiview (which by default is 320 x 460).
I've tried setting the view containing the tableview to a height of 900 on viewdidload and setting it to sizeToFit, hoping it would shrink as tablecells are added to the tableview (assuming as cells are added the tableview's frame would scale appropriately). But this doesn't seem to work.
Any thoughts?
Thanks!
I just ran into a similar situation (UIScrollView -> UITableView) and was banging my head against it for hours. The short answer is this: the correct sizes to use as the basis of sizing things out were not available until my ViewController (governing the UIScrollView) was sent viewWillAppear:. Before then, I found that the values were set, but often wrong. (E.g., my first attempt at doing this was in viewDidLoad:, and the sizing values were changed there, but they were wrong!)
So my solution was thus:
In viewDidLoad: cause your enclosed UITableView to reloadData once you have your data in place.
in viewWillAppear: do the following (presuming tableView is the enclosed UITableView and scrollView is the enclosing UIScrollView):
tableView.frame = (CGRect){
tableView.frame.origin,
tableView.contentSize
};
// Finally, set content size based on the last element in the scrollView
// in my case, the last element IS the tableView, yours might be different
scrollView.contentSize =
CGSizeMake(scrollView.width
- scrollView.contentInset.left
- self.scrollView.contentInset.right,
tableView.top
+ tableView.height);
N.B., I was getting some very weird numbers when I checked them before viewWillAppear:; e.g., the tableView.contentSize seemed to be the product of default rowHeight * number of rows, ignoring my custom row heights and excluding any separators! Very odd.
This is just a guess, but since UITableView is a subclass of UIScrollView, can you use the contentSize property to see its overall size after a call to reloadData? You could use the size of the tableview to resize the UIView and the contentSize of the scrollView.

UITableView Did Load (Done drawing the cells)

Question
How can you detect when the Table View is done drawing the cells?
Issue
I got two labels within the contentView of an UITableViewCell. The size of these labels are dynamic. I was able to do so by subclassing UITableViewCell, in the drawRect method I adjust the frames of the two labels depending on their content. Now I want to align all the second labels.
My Thoughts in Steps
Determine the content in the table view and let it load automatically.
Run through the table view cells and determine the x position of the second label within the UITableViewCell that is the furtherest away.
Store this x position and when any cell is drawn use this x position to place the second label.
The problem is that if I use the following code:
for (int row = 0; row < [self.tableView numberOfRowsInSection:section]; row++) {
UITableViewCustomCell *cell = (UITableViewCustomCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]];
NSLog ([cell.labelTwo description]);
}
The second label has not yet been drawn, meaning I can't determine the size of the frame and thus can not find the proper x position to align all second labels.
I have tried subclassing the UITableViewController and looking at events such as viewDidLoad and viewDidAppear unfortunatly also in these events the cells aren't drawn yet.
What I Want ...
What I want is for the table view to draw the cells at least once so I can determine the sizes of the labels within the table view cell. I thought to accomplish this by looping through all the cells with cellForRow, but although it successfully returns the cell the content is not drawn yet meaning the frame remains with a width of zero.
Does anyone have a solution?
Thanks in advance,
Mark
Try calling sizeWithFont: on the contents of these labels to get the max width before you draw anything. You should be able to use it later in your cellForRowAtIndexPath: to adjust the width as you need.
I would recommend you reconsider using UITableViewCellStyleValue2 cells instead and attempt to configure the textLabel and detailTextLabel. I had a similar situation and this is how I did it.
First off, you really ought to just pick an explicit, fixed position at which the first label ends and the second one begins, based on what you know about the minimum and maximum lengths of the text that will be put in those labels. That would eliminate this problem entirely.
But if you want a solution: use the sizeWithFont: method or one of its cousins (see the Xcode docs). Specifically, loop through the values that will go in the first labels, apply sizeWithFont to each, and keep track of the largest width you see. (I'm assuming you have access to the values before they go in the cells; since they're dynamic, they must be passing through the table view controller, no?)
Now you have the value you seek, without having to perform the extremely wasteful operation of creating a bunch of cell objects and never using them for their intended purpose.
I think what you need to do is to add a viewController to the have the UITableViewController control the UITableViewCell itself so that you can capture the events of the labels loading. The viewController will have references to both labels so it can adjust them accordingly in response to -viewDidAppear.
I've never done this but a UITableViewCell is a view like any other so you should be able to set up a controller for it. You might need to manually activate the controller since you have no navigation controller to do it for you in this context.

UILabel inside custom UITableViewCell not drawing at the correct size

I have a custom table cell which contains a number of UILabels. At runtime, I am adjusting the height of the labels to fit their contents using sizeWithFont:constrainedToSize:lineBreakMode: and repositioning them accordingly. The last label in the cell contains a large amount of text, causing it to wrap, and I'm having a very odd problem. Although the sizeWithFont call returns the correct size, and I'm setting the UILabel's frame to that height, it draws a couple of lines short. This screenshot illustrates what I'm talking about:
In this example, the height of the full block of text should be 90 (as checked in Interface Builder), and that's what returns from sizeWithFont. It's also the height that the UILabel's frame is set to, which I have verified by logging and also by stopping execution and inspecting the value. However, as you can see, it's clearly not drawing the full 90 pixels high, although it's correctly allocating the space for it (the thin black line above 'Edited' is the table cell border). I'm completely perplexed. If anyone can offer some insight as to why it's behaving this way, I would be very grateful.
At last, a solution!
Turns out that the cell does layout twice -- once during heightForRowAtIndexPath, which is where I tweak all the heights of the subviews and the cell, and later during some untraceable transaction originating in __CFRunLoopDoObservers. How did I trace this? I added a layoutSubviews override to my custom table view cell class so I could breakpoint it.
During the second pass, the last UILabel subview was getting set to a shorter height than I set it to, probably in accordance with some arcane autoresizing rules. (Yes, I tried tweaking all of those settings first, with no success.) As it turns out, merely doing nothing in layoutSubviews disabled this framework behavior, allowing me to completely control how my views draw.
With iOS 8 it doesn't work anymore like this. Implementing layoutSubviews alone doesn't do the trick, because the layout of subviews have already changed when the method is called.
I have found 2 solutions:
adding NSLayoutConstraint to layout the subviews programmatically
implementing subview's layoutSubviews and change the frame
An example für solution 2:
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect frame = self.frame;
frame.size.height = 39.f;
self.frame = frame;
}
I've fought with similar problems. It was to do with other properties being set in previous incarnations of the cell. To find it / prove it I changed the reuseidentifer for the offending cell to make sure it was a unique cell.