Set UIButton Frame at the center of UIImgeView - iphone

Here is my code to set at the center
UIButton *videoImage = [[UIButton alloc]initWithFrame:CGRectMake((secondImageView.frame.size.width/2)-50,(secondImageView.frame.size.height/2)-50,50,50)];
videoImage.transform = CGAffineTransformMakeRotation(ang*(3.14/180));
[videoImage setBackgroundImage:[UIImage imageNamed:#"PlayButton.png"] forState:UIControlStateNormal];
[videoImage addTarget:self action:#selector(PlayMusicOnClickofButton:) forControlEvents:UIControlEventTouchUpInside];
[secondImageView addSubview:videoImage];
videoImage.tag = k+1000;
secondImageView.userInteractionEnabled = YES;
[videoImage release];
The problem that i am facing is when i change orientation of button it's position gets changed. It doesn't remain at center. Please help
Thanks

Actually you should use this method....
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
//handle how you want to show in landscape mode
}
else
{
//handle how you want to show in portrait mode
}
return YES;
}

You need to handle this. What you are doing is correct. You need to do tweak it when the orientation changes as the dimensions of your parent view change. Implement willAnimateRotationToInterfaceOrientation delegate. check this out -
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft||toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
//handle how you want to show in landscape mode
}
else
{
//handle how you want to show in portrait mode
}
}

Related

Resizing in iOS 6

I am brand new to iOS development, and I am trying to implement a rotatable and resizable user interface. The problem that I am encountering is re-setting the position of UI elements upon rotation. I can't even get the simple code below to work properly:
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (BOOL)shouldAutorotate {
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
self.button.frame = CGRectMake(502.0, 207.0, 73.0, 44.0);
} else {
self.button.frame = CGRectMake(197.0, 69.0, 73.0, 44.0);
}
}
I have de-selected the Autoresize Subviews option; however, the button still ends up in the same spot. I have logged the frame position, which appears to have the correct coordinates, but it is definitely not ending up that way.
in the first panel of Utility inspect. uncheck the use auto-layout.

rearranging UIButtons while orientation changes in iphone?

enter image description herehai, i have a list of buttons in a view which is a sub view of another UIView. i need to rearrange these buttons in order while the device orientation changes to landscape mode.
is their any way to do this? or else i need to do all these things manually by repositioning all buttons?
shouldAutorotateToInterfaceOrientation:, mentioned in the other answers, is for letting your view controller decide whether or not to allow autorotation to a given orientation. (you're supposed to return YES or NO).
If you really want to programmatically adjust layout of your subviews, you would do it in either
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[UIView animateWithDuration: duration
delay: 0.0f
options: UIViewAnimationOptionCurveLinear
animations: ^(void) {
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
self.button.frame = CGRectMake(x1, y1, self.button.frame.size.width, self.button.frame.size.height);
} else {
self.button.frame = CGRectMake(x2, y2, self.button.frame.size.width, self.button.frame.size.height);
}
}
completion: ^(BOOL finished) {
// code to run after animation completes can go here
}];
}
or, you can use willAnimateRotationToInterfaceOrientation:duration: for one-step rotations.
Not only is it only called when the autorotation actually happens, but it gives you the duration of the animation. This allows you to wrap any animatable view property changes inside your own animation, so that they smoothly animate during the rotation, instead of just snapping to their new values.
But, really, this is usually not the best way to solve the problem. See this link for a more general discussion of the issue
Change the CGFrame of you buttons in the
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation==UIInterFaceOrientationPortrait)
{
Button1.frame=...;
Button2.frame=....;
}
else if(interfaceOrientation==UIInterFaceOrientationLandscapeLeft)
{
Button1.frame=...;
Button2.frame=....;
}
return YES;
}
1-you can use this
YOUR_OBJECT.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin;
OR
2- use frame property with CGRectMake for your object in
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
if(interfaceOrientation==UIInterFaceOrientationPortrait)
else if(interfaceOrientation==UIInterFaceOrientationLandscapeLeft)
You can handle it via
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
method.
Make two methods in your viewController.h class assume they are -
-(void)viewForPortraitMode;
-(void)viewForLandscapeMode;
And this is the body part of both in your viewcontroller.m file.
//Set frame according to you
-(void)viewForPortraitMode
{
[btn1 setFrame:CGRectMake(117.0, 383.0, 87.0, 37.0)];
[btn2 setFrame:CGRectMake(40.0, 20.0, 240.0, 324.0)];
}
-(void)viewForLandscapeMode
{
[btn1 setFrame:CGRectMake(200.0, 252, 87.0, 37.0)];
[btn2 setFrame:CGRectMake(20.0, 7.0, 446.0, 228.0)];
}
And call these two methods in this
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
[self viewForPortraitMode];
}
else
{
[self viewForLandscapeMode];
}
return YES;
}

Changing iphone orientation, then changing it back cuts my view in half?

I have a view controller in my iphone app setup so that when I change the orientation from portrait to landscape, I change the view. When I change the orientation back from landscape to portrait, the initial view comes back, except this time it is all crammed into the left hand side of the screen. Eventually, when I change orientations enough times everything disappears completely. Is this a common issue beginners have? What could I be doing wrong?
In my root controller I am allowing the orientation to change only when a specific view is being shown with this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (self.currentView == (NSInteger*)Chart || self.currentView == (NSInteger*)Detail) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
return (interfaceOrientation == UIInterfaceOrientationPortrait);
Where self.currentView is an enum of what view I currently have up. The Detail view I want to make sure stays as a portrait view, but when I change the orientation while on that view I want it to change the view to the Graph. Again, this works fine the first time, but when I change back from Graph to Detail, it crams all the controls on the Detail view to the left hand side of the screen.
Here is how I'm changing the view:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (self.currentView == (NSInteger*)Detail && (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"changeView" object:self userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:Chart] forKey:#"view"]];
}
if (self.currentView == (NSInteger*)Chart && (toInterfaceOrientation == UIInterfaceOrientationPortrait)) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"changeView" object:self userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:Detail] forKey:#"view"]];
}
#justin I once did this which got me into same situation as you are. May be you can check if you haven't done something like this
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
CGRect rect;
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
rect = CGRectMake(tableView.frame.origin.x,tableView.frame.origin.y,
tableView.frame.size.width - 50, tableView.frame.size.height - 30);
}
else {
rect = CGRectMake(tableView.frame.origin.x,aBar.frame.origin.y,
tableView.frame.size.width, tableView.frame.size.height);
}
[tableView setFrame:rect];
return YES;
}
All I wanted was a table view with small frame in Portrait mode, without saving the original Frame I was trying to reduce its width and height which eventually brought the table view to a very small size after multiple rotation..
Lolzzz. I should have first saved the original tableview frame and then done something like this
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
tableView.frame = CGRectMake(tableView.frame.origin.x,tableView.frame.origin.y,
tableView.frame.size.width - 50, tableView.frame.size.height - 30);
}
else {
tableView.frame = originalTableViewFrame;
}
check if you have autoresizeSubviews ON (in XIB/Inteface Builder) on your view and possibly parent views and try to turn it off if you are changing frame manually , this solved it for my case

UIView - Center alignment all the contents of UIView?

How to center alignment all the contents of view. When i am rotating iPad contents are not align center it self how to resolve this problem??
Thanks!!!
Provide an implementation of the didRotateFromInterfaceOrientation method and center all subviews.
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
for(UIView *subview in [self.view subviews]) {
subview.center = self.view.center;
}
}
You should change programmatic-ally the contents' frame for both Landscape and portrait modes.
You can try this as:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
// set contents' frame for landscape
}
else {
// set contents' frame for portrait
}
return YES;
}

UITableView in popover doesn't stop scrolling

I have a UITableView being shown in a popover. One cell in my table view has a UITextField in it. When the text field is being edited (the keyboard is visible) and I rotate the device from portrait to landscape and then try to scroll the table view, it keeps going past its bounds, instead of stopping and "bouncing".
Does anyone know how to fix this?
You can try to detect when the device is rotated and call the tableViewController method that adjusts scrolling position of selected cell to the area you need.
You can use following code when rotate device
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if ([appDelegate.aPopover isPopoverVisible])
{
[appDelegate.aPopover dismissPopoverAnimated:NO];
CGSize size = [self rotatedSize];
size.height -= [[self.view viewWithTag:154] frame].size.height;
appDelegate.aPopover.popoverContentSize = size;
[appDelegate.aPopover presentPopoverFromRect:[[self.view viewWithTag:154] frame]
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
}
}
// for set popoverview size when rotate screen
-(CGSize)rotatedSize
{
UIDeviceOrientation orientation1 = [[UIDevice currentDevice] orientation];
UIInterfaceOrientation toInterfaceOrientation = orientation1;
if ((toInterfaceOrientation == UIInterfaceOrientationPortrait) || (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
{
return self.view.frame.size;
}
else
{
return CGSizeMake(self.view.frame.size.height, self.view.frame.size.width);
}
}
Thank you,