How to find dynamic centre of iOS device screen [duplicate] - iphone

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
iOS SDK: Moving the button into the center of screen by code
UIImageView. Zoom and Center to arbitrary rectangle. Can’t determine correct center on screen
I'm trying to centralise an UIActivityIndicator in a UIWebView(which in turn is a subView of a UIScrollView, which also has a UIToolbar element to the left - although the left-toolbar isn't always visible)
Similar questions have been asked before, but the point is in finding the center "dynamically" i.e. on change of orientation as well as presence or absence of the left toolbar.
What's the best approach? Is there any better way to do this than overriding the method shouldAutorotateToInterfaceOrientation?

Center of what btw?
You can access the UIViewController's center as self.view.center
or in your case, UIWebView's center as yourwebView.center
And by giving it resizing elements of top left, activityindicator would center it-selves always.
EDIT :
If you want center of the screen that would be gained by frame like
Consider activity indicator of width and height 30, 30.
(([UIScreen mainScreen].bounds.size.width)/2 - 15, ([UIScreen mainScreen].bounds.size.height)/2 - 15, 30, 30);
Then, set autoresizing elements to none.
Go to size Inspector and remove all the arrows from the Auto-sizing feature.

There can be not general solution to your question. This is because only you know what element in your view you want to ignore or not. The view always stays the same, no matter what element you add to it. So, I suggest getting a rectangle for the part of the view you consider as clear/empty/available and setting your loading indicator in that. Just get the view's whole frame (self.view.frame.size.height) and substract any elements from there. For example
MyIndicator *indicator = [[MyIndicator alloc] initWithFrame:CGRectMake(self.toolbar.frame.size.width + self.toolbar.frame.origin.x, self.topBar.frame.size.height + self.topBar.frame.origin.y, self.view.frame.size.width - self.toolbar.frame.size.width - self.toolbar.frame.origin.x, self.view.frame.size.height - self.topBar.frame.size.height - self.topBar.frame.origin.y)];

Related

Can't have a UIViewPicker well laid for iphone 4 & 5

I have one storyboard file for iPhone devices, and in one of the views there is a subview that contains a UIPickerView, and, when it runs on the iPhone 4 the UIPickerView is stuck at the bottom of the view as it should be, but when it comes to the iPhone 5, the UIPickerView appears a little above of the bottom of the screen.
If I fix the problem for the iPhone 5, the UIVPickerView won't appear completely when I run it on the iPhone 4. (half of it appears below the screen)
So is there a way to have the UIViewPicker well laid for both iphone 4 and 5 resolutions?
NOTE: I solved the issue by creating a completely new subview with the UIPickerView. Now it is well laid whatever the device is.
When creating the picker, you need to use the size of the screen to determine the location. It seems like you are just putting in the coordinates for the y manually, which only works when using one screen size.
If your view that you are in is the same size as the screen, as it most likely is but may not be, you can do:
int y = self.view.frame.size.height;
and use that as the y-coordinate of your pickerView.
Otherwise, you can find the size of the screen by using:
[[UIScreen mainScreen] applicationFrame].size.width //if in portrait
//or
[[UIScreen mainScreen] applicationFrame].size.height //if in landscape
Then you have to subtract the height of the navigationController from that if you have one.
If you're using autolayout, you should be able to change the constraints the picker uses to get it to stick to the bottom. Select the picker, click the "H"-shaped autolayout menu icon in the bottom right corner of the storyboard, and choose "Bottom Space to Superview". Then delete any constraints attaching the picker to the top of the screen. If that doesn't work, make sure that all of the picker's superviews have constraints to attach them to the bottom, too; you'll have to decide whether you want them to resize or slide down on an iPhone 5.
I fixed the issue. Not sure what was going wrong but I deleted the subview, then created a new one and added the UIPickerView. Surprisingly, now it is well laid whatever the device is.

How to create resizable view with handlers on sides to stretch or resize in ios? [duplicate]

This question already has an answer here:
Changing View size using user touch input
(1 answer)
Closed 7 years ago.
I am creating an app where user touch the screen and places a rectangle view on screen which he can move or resize by touching its corners like any other crop view . I have done this functionality but i want eight handlers on the sides of that rectangle so that user can use that handlers to resize it. Now i am unable to create such functionality . I tried to add eight buttons as subview on that rectangle view but when resizing when that view increase its size than all eight buttons just change their positions . can anybody help me with this functionality ?
Thanks in advance
Hi i got the answer from here . here is a great class for making resizable view. Great work by the developer
Create resizable view in ios with UITouch
To do this you are going the right way about it.
You need to create a custom view that positions these eight handles depending on the size of the view. When the view changes size you then need to reassess where those handles are so maybe have a function thats called when the view is resized and in here you change the location of each handle just by changing its x and y origin depending on the view size.

iPhone Infinite Vertical Text Scrolling [duplicate]

I have a UIScrollView of size 320*460 and with content size 1024*1024.
I can place 25 images of 256*256 in it with the 13th picture shown at the centre of the
screen when it loades with bits of surrounding pictures around it.
When i swipe to any side I want it to appear just like the mapView. With new images
appearing and showing.
How can I do it?
It's very easy & somewhat tricky...
You don't need to define the specific size for the ScrollView.....
Generally we use to define ....as per the Examples of Apple
//#define SCROLLVIEW_CONTENT_HEIGHT 460
//#define SCROLLVIEW_CONTENT_WIDTH 320
which is of no use...if you need the infinite height..
just set the height of the ScrollView dynamically as per the Objects added to the ScrollView....
No need to set the predefined height for this...take the dynamic height....
scrollview.contentSize = CGSizeMake(320, txtView.contentSize.height+450);
CGPoint topOffset = CGPointMake(0,0);
[scrollview setContentOffset:topOffset animated:YES];
Hope this will surely work for you..
Good Luck :)
Checkout the sample code called 'Tiling'. It might be what you're looking for.
When scrolling stops, couldn't you just adjust the bounds so that you are now "re-centered" with your new offset?
Presumably you can't scroll more than so many pixels before your finger hits the edge of the screen, so you only need bounds that a few hundred pixels outside the original center.
Once you have a new center, adjust your bounds accordingly, retiling as necessary.

Hiding elements outside of bounds on iPhone

I'm trying to get a UIView to expand (with animation), sort of like an accordion menu. I can get the animation working fine however the issue is the subviews of the UIView are expanding past the bounds of the UIView.
For example, the view has an UILabel in the top-right corner with a height of 16. Assume the UIView height is 0 at the beginning of the animation. One would expect that the content of the view be hidden and gradually reveal as the UIView grows. Once the height has reached 8, for example, half the label should be visible. However that isn't the case - rather, the label is visible the whole time, regardless if it's height extends outside of that of it's parent view.
Any ideas how to resolve this?
Okay, I had to set the clipsToBounds property to true. I spent some time googling before making the question but didn't have much luck until I saw it in the Related section of my question.

Moving a UIView containing UIButtons makes those buttons stop respond to taps

I have a UIView in a UIScrollView in a UIView in a .xib that contains several buttons. If I move that UIView down by several pixels from within viewWillAppear, the buttons all stop responding to taps.
Here's the code I'm using to resize the UIScrollView and shift the buttons down:
// adjust the view height to accomodate the resized label
scrollView.contentSize = CGSizeMake( 320, 367 + expectedLabelSize.height - originalLabelSize.height );
// Adjust the location of buttons
CGRect buttonsBounds = buttons.bounds;
buttonsBounds.origin.y -= expectedLabelSize.height - originalLabelSize.height; //XX
buttons.bounds = buttonsBounds;
If I comment out the line marked XX, the buttons work just fine, but are in the wrong place of course.
If I try various numbers of pixels (replacing expectedLabelSize.height - originalLabelSize.height with a hardcoded value), I get interesting results. 10 pixels works fine. 50 pixels causes my top button to work fine but my bottom one to fail. 100 pixels and both buttons fail. (-50) pixels causes the bottom button to work fine but the top to fail.
Any idea what might be causing the problem? Do I somehow need to inform the buttons that their parent view has moved?
The problem was that I should have been using buttons.frame instead of buttons.bounds. Using the frame instead fixed the problem.
I suggest giving the parent UIView a background color and setting the button style to Rounded. That way you can actually see what is happening on the screen. (Assuming you are using transparent buttons on top of some image or so)