Adding and Removing UIView From Springboard - ios

I am developing a little MS tweak that adds a view on the Springboard. I want it to be constantly updated so I called it into the
- (void)showSpringBoardStatusBar
Then I create and add the view using this:
[[UIApplication sharedApplication].keyWindow addSubview:view1];
Is this the right way?
But the problem is this view uses a low alpha level to be transparent and every time the view is updated by the showSpringBoardstatus bar another UIView is added onto of it eventually just making the view solid. This also is uneconomical in terms of memory. so then I went back an added what I thought would remove the code:
view1 = nil;
[view1 removeFromSuperview];
But it seems like this doesn't make a difference as it is still there and nothing changes.
I have been searching for the last few days for anything to help me with this but got nothing out of it. All I can think of is that I can't remove an added subview from a key window like I would from a normal view or I just don't how to do it correctly.
Any help would be appreciated. Thank you.

Setting view1 to nil and than calling "removeFromSuperView" might be a bad idea. How about adding the view only once, keeping a reference and updating this reference constantly without adding it to keyWindow again?

Related

Take a snapshot of a hidden UIView

I'm trying to take a snapshot of a hidden view but am running into several issues. If I try unhiding it quickly, taking a snapshot, and then rehiding it, I sometimes get a quick flicker on the screen that is pretty jarring.
toCollectionViewCell.hidden = NO;
UIView *toPlaceHolderSnapshot = [toCollectionViewCell resizableSnapshotViewFromRect:toCollectionViewCell.bounds afterScreenUpdates:YES withCapInsets:UIEdgeInsetsZero];
toCollectionViewCell.hidden = YES;
I'm pretty sure the flicker is caused by the afterScreenUpdates:YES, but I can't imagine that is intended behavior.
I've also tried moving the cell/view off screen instead of hiding it, but I can't be certain when that cell might be reloaded and therefore moved back into its place prematurely.
Is there a way to take a snapshot of a hidden view or a more clever way to achieve this? I need this functionality during a custom transition animation where I am pulling a collection view cell out of the collection view and then returning it back into place on dismiss. I am taking snapshots of the before/after state and then transitioning between the two during the animation.
Thanks!
Add an extra container view to your view hierarchy. Hiding the container will have the same visual effect, but you'll be able to snapshot the content of a container with ease.
I've also tried moving the cell/view off screen instead of hiding it, but I can't be certain when that cell might be reloaded and therefore moved back into its place prematurely.
This approach is probably the simplest. As long as all of your work is done on the main thread, the cell won't move during your snapshot.
You could also try archiving and then unarchiving the view (to essentially copy it):
id copyOfView =
[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:originalView]];
UIView *viewCopy = (UIView *)copyOfView;
viewCopy.hidden = NO;
(All views in the hierarchy will need to conform to the NSCoding protocol.)
Finally, you could draw your cell to a UIImage, and then display it in a UIImageView. Sample code here.

Who should control the presenting of a new UIView?

Ok, I have a somewhat trivial question that bugs me. When I present a new UIView, should that view control how its displayed and removed?
It feels like that would be a good idea, the view becomes self contained. Similar to UIAlertView:
[alertView show];
But on the other hand, it feels like something else should control it and should not be left to itself.
[self.view addSubview:newView];
[UIView animateWithDuration...];
Or is this 6 one way, half a dozen the other?
UIAlertView is a special case—it gets added to its own window, not any existing part of your view hierarchy. If you’re not doing anything special like that, your second example is the correct approach.
That said, if this view is a custom class that you’re going to use in multiple places and want to behave consistently, it should control its own presentation. For instance, UIActionSheet’s -showInView: method takes care of adding itself as a subview, positioning itself, animating in, and so on.

Flipped NSScrollView problems

I have an NSScrollView that needs to display a variable amount of NSViews in it. I made a custom NSView that has isFlipped return YES and put my NSViews in that before I set it to be the NSScrollView's documentView. That works well. It displays my items top to bottom like I would expect.
But, when the contents changes and I need to change the NSScrollView's documentView's frame, my contents disappears.
This would be WAY easier with a UIScrollView, but alas.
I'm assuming these are equivalent:
[NSScrollView.documentView setFrame:newFrame];
[UIScrollView setContentSize:newSize];
I would imagine that a lot of coders need to have a flipped NSScrollView, but how do you deal with changing the content size without this madness? Obviously, I'm missing something.
Anyone? Thanks!
can you try just setting
[scrollView.documentView setFrameSize:newFrame.size];
to see if your content disappears?
The problem was with the internal views and autoresizing/constraints. My internal views are actually a series of view controllers with their own xib file. In each view controller, I added the following line:
[self.view setAutoresizingMask:NSViewNotSizable];
And that solved everything.
Special thanks to lead_the_zeppelin for helping me (in chat) go through everything.

UIView with UIButtons not showing up prior to click or rotate

I've been banging my head with this issue for the last two days. Googled a lot but wasn't able to find the answer yet, so I decided to request some help here. Let's see if I get any luck.
I'm coding a reusable control that consists of an UIView with a variable number of customized UIButtons. I implemented initWithFrame:, initWithCoder: and drawRect: where the buttons (which are built prior to drawing) are actually added to the view. Everything is done programmatically since the UIButton content should be supplied when using the control, so there's no XIB for this UIView.
This UIView, let's call it CustomizableBarButton is then used in an UIViewController, let's call it MyTestViewController with a view on it, let's call it customizableBarButtonView.
MyTestViewController's GUI was set on IB where an UIView was tied to customizableBarButtonView (the class was matching accordingly).
MyTestViewController's is a pretty standard class except for the viewWillAppear: that initializes the buttons and passes them to the CustomizableBarButton along with some other options.
The issue is that everything works perfectly...except for the first time!
I mean, when I run the app on the simulator (I haven't tried it on the iPhone yet but I strongly believe that it's not an hardware issue) the MyTestViewController shows the customizableBarButtonView background but not the buttons. Now when you click on the place where a button should be all the buttons suddenly appear!
I'm puzzled since the CustomizablebarButton drawRect: runs before the strange "click n'appear" effect and the buttons are actually added to the subview.
Another hint that my help: if you don't click on the buttons (so you still got no buttons yet) but rotate the device they will also appear as if by magic!
It is probably something very simple but I'm missing it and I'm going nuts...
Can someone lend a hand on this, please?
Thanks in advance!
You said you're adding the buttons in drawRect:. Don't do that. You need to add the buttons in your init methods.

iOS: Using modal pageFlip makes strange cell animation in UITableViewController

I wanted to show a Settings view, so I figured I could use the pageCurl modal style. I have a UIViewController that I am presenting from:
Settings *settings = [[[Settings alloc] initWithStyle:UITableViewStyleGrouped] autorelease];
settings.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
settings.modalTransitionStyle = UIModalTransitionStylePartialCurl;
settings.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:settings animated:YES];
When the page curls, to reveal my UITableViewController below, the cells subviews (labels and accessory views) seem to animate into position, which looks horrible:
The strange thing is, is that the animation never happens to the top cell in a section. This does not happen with other modalTransitionStyle's so I am wondering why this could be happening. It is really becoming frustrating because I cannot figure out how to stop this from happening.
Ideas?
If you set the autoResizingMask property of the tableView to UIViewAutoresizingNone, I think this won't happen. This may be a little bug in the page curl animation.
I've noticed that the direction of this weird animation (which also happens to UIButtons), depends on the Alignment chosen in the Control section of the object's Attributes inspector (in Interface Builder). I've been tinkering with all attributes all evening and have found no solution yet. :(
Edit: so, I had three UIButton in my view (and one UIPickerView) that where doing this weird behavior at the same time. I created a property linking to ONE of the UIButtons. Then, in the viewDidLoad of this view's controller I added the following line:
_firstAffectedButton.titleLabel.autoresizingMask = UIViewAutoresizingNone;
And that fixed it for me, for all the buttons and the picker!! :? Weird, right?
Can anyone explain why this is working? I have the feeling I'm doing something stupid to fix this, but I can't see what it is.
Edit 2: Turns out there was a better and cleaner solution to this problem. See this post.

Resources