making buttons change size along when zoom - objective-c

I have a UIScrollView, with a UIImageView and UIButton inside of it.
I was reading in the answers here that if I have a single view that contains all of my zoomable content, then I should be fine. The problem is that the button doesn't zoom when the image does.
I thought that what I have would work, since the Scroll View is the zoomable view.
Do I need to create another UIView and put everything in there (and keep the same hierarchy?) ?

Have you checked your Springs and Struts? Look in the size inspector under "autosizing". Those little red lines.. if that doesn't make sense, search for 'auto layout'.

I ended up fixing this by declaring the button in code, and added a UView into the hiearchy. The uiview is a subview of the scroll view, and the imageview is a subview of the uiview. I then added the buttons to the uiview.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
...
[view addSubview:button];

Related

UIView in UITableView disappear when becomes firstResponder

In storyboard I have a UITableViewController-->UITableView-->UITableViewSecion--> with static cells
in the same UITableView I also have a UIView that holds a background image and UITextView.
A click on a button shows the UIView and set it's frame, which appears OK, but as soon as I click on the UITextView or make it firstResponder programmatically the keyboard appears and the view disappears
the code when clicking the button
self.myView.frame = CGRectMake(0, self.tableView.contentOffset.y+100, self.tableView.frame.size.width, self.tableView.frame.size.height-100);
self.myView.hidden = NO;
how can I fix this?
Can you copy paste us the code where you add the view to the tableview? Are you doing it with constraints or with frames?
The issue you are having is probably due to the fact the UITableViewControllers automatically shrink the contentSize of the UITableView they hold when the keyboard shows. If you add a UIView to your tableView with addSubview: programmatically, you might need to add a flexible bottom resize mask to make sure when the contentSize shrinks in height, your view stays attached to the top and not the bottom.
Try this on viewDidLoad:
[theViewYouAddedToTableView setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin];

How to generate a image of uiscrollview?

I have a UIScrollView with subview like UILable and webviews. So when I click on a button all the content of the scrollveiw should generate as a image. I tried to generate it but it is not getting all the content of UIScrollView it is just showing only visible view.
and my code is
[Scroll.layer renderInContext:UIGraphicsGetCurrentContext()];
It is because your scrollView's contentSize is larger but its scrollView.frame is still smaller.
Instead of scrollview, add your content first into a UIView (Let say it containerView). containerView should have height-width stretchable according to your content.
After that add your containerView to scrollView. So your scrollView require contentSize = containerView.frame.size
Now try to taking screenshot for the IBOutlet of containerView, not the scrollView.
I have not tried this out, but I guess it should work in this case.

UIScrollView not scrolling when adding item in IB and code

When I create a Scrollview on my scene, and then add a button to scene in IB. Then I go into the code, set the content size, enable user interaction and add another button. When I run the program in the simulator the Scrollview does not work, if I remove the button that is in IB on the scene it works just fine. Is it not possible to add items to the scrollview both in IB and programmatically?
EDIT: I thought it may be something in the app I already had. So I decided that I would create anew project and all it has in it is code, and the scene picture below. It is indeed added below the ScrollView.
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton setTitle:#"My Button" forState:UIControlStateNormal];
myButton.frame = CGRectMake(100, 100, 150, 50);
[scrollView addSubview:myButton];
scrollView.userInteractionEnabled = YES;
[scrollView setContentSize:CGSizeMake(320, 1000)];
Here your scrollView is not scrolling due to the autoLayout, uncheck the auto Layout if you are not using.
I Just made a similar to your requirement. It is working fine, and after allowing autoLayout it just stopped scrolling.
The auto layout constraints fits to the visible part of the screen, if the objects in scrollView are more then screen size, it will scroll.
So my suggestion if you are not using autoLayout just uncheck it, and works fine.
Here is an helpful link: http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/_index.html#//apple_ref/doc/uid/TP40012166-CH1-SW19
Basically what this says is that, with auto-layout you don't and shouldn't have to use setContentSize:. Instead your inner view should have it's edges snapped to the edges of the scrollview. Let me demonstrate how I solved it.
scrollBackground : a view that contains every other view that needs to scroll in the scrollview.
[scrollview addSubview:scrollBackground];
[scrollview addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"V:|[scrollBackground(1000.0f)]|"
options:0 metrics:nil
views:NSDictionaryOfVariableBindings(scrollBackground)]];
[scrollview addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"H:|[scrollBackground(==scrollview)]|"
options:0 metrics:nil
views:NSDictionaryOfVariableBindings(scrollBackground)]];
The key here being those | at beginning and end of the VisualFormat String. These will tell the scrollview what is the contentSize. Of course if you have a contentSize smaller than the frame size of the scrollview it won't scroll in that direction. In my example this is true for width. It only scrolls up and down.
Make sure you set your content size in viewDidLoad or at some point after the view has already been loaded from the nib file.
As vishy Pointed out, your Button should be part of the ScrollView's Hierarchy, else you'll just be scrolling an empty view.
In the case you posted, the scrollview will not scroll because all of the content is visible. Try changing that last line to:
[scrollView setContentSize:CGSizeMake(50, 50)];
and it will start scrolling, because the content size is smaller than all of the content in the view.
check d2burke answer in this question
you should place your code in viewWillLayoutSubviews instead of viewDidAppear

How to dynamically reposition UIButton as subview of UIImageView when rotating

I'm working on an iPad app that lets you control different things in a prototype of an intelligent house. For example it lets you turn lights on and off. For this, I have made a UIImageView that shows the floor plan of the house and added UIButtons as subviews for each lamp that can be toggled.
As you can see the buttons are placed perfectly on the floor plan using the setFrame method of each UIButton. However, when I rotate the iPad to portrait orientation, the following happens:
The buttons obviously still have the same origin, however it is not relative to the repositioning of the image.
The floor plan image has the following settings for struts and springs:
and has its content mode set to Aspect Fit.
My question is
how do I dynamically reposition each UIButton, such that it has the same relative position. I figure I have to handle this in the {did/should}AutorotateToInterfaceOrientation delegate method.
It should be noted that the UIImageView is zoomable and to handle this I have implemented the scrollViewDidZoom delegate method as follows:
for (UIView *button in _floorPlanImage.subviews) {
CGRect oldFrame = button.frame;
[button.layer setAnchorPoint:CGPointMake(0.5, 1)];
button.frame = oldFrame;
button.transform = CGAffineTransformMakeScale(1.0/scrollView.zoomScale, 1.0/scrollView.zoomScale);
}
Thank you in advance!
I find the best way to layout subviews is in the - (void) layoutSubviews method. You will have to subclass your UIImageView and override the method.
This method will automatically get called whenever your frame changes and also gets called the first time your view gets presented.
If you put all your layout code in this method, it prevents layout fragmentation and repetition, keeps your view code in your views, and most things just work by default.

Releasing invisible buttons with images in UIScrollView

I have main UIScrollView with lots off buttons which i create like this:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]
every button have an image:
UIImage *fileImage = [UIImage imageNamed:#"sun.png"];
[myButton setBackgroundImage:fileImage forState:UIControlStateNormal];
Buttons count could be more than 500. So i need to remove from UIscrollView invisible buttons with images to save memory ?
I believe in this method i need to calculate when UIscrollview is stopped scrolling and for example 20 images are invisible, then i need to remove them and reduce scroller contentOffset.
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
}
Maybe some one could give me tips on this. Or even have a good bookmarked tutorial.
I created a similar thing to this using UIViews in a UIScrollView. When the UIScrollView loads, I set the contentView size of the scrollView to be the size of all the views but only loaded the views that can be seen, then when the user scrolls i added the previous/next views and removed the hidden views.
This question helped me: How to implement UIScrollView with 1000+ subviews? especially akosma's answer