UIScrollView setContentSize wrong in iPad? - objective-c

I'm integrating UIScrollView in my games and I notice the setContentSize seems like having some problem when I goes from iPhone to iPad.
Basically I scale the contentSize based on how many contents I have in the UIScrollView
CGSize newContentSize = CGSizeMake(0.0f, totalContent * contentHeight);
[scrollView setContentSize: newContentSize];
This codes work on iPhone retina, but when it goes to iPad, the contentSize has gone larger.
I check the value and it is correct (because in iPad the resolution has been doubled) Problem
is when I try to scroll until the bottom, it will display a lot of empty spaces after the last content.
Does anyone know what happen to this?
EDIT:
The complete codes are as below, it's not a normal app, it's using cocos2d game engine
CCSprite* image = [mImageArray objectAtIndex: 0];
CGSize newContentSize = mScrollView.contentSize;
newContentSize.height = mImageArray.count * image.contentSize.height;
[mScrollView setContentSize: newContentSize];

Related

iOS9 covering status bar with custom uiwindow - wrong position

I am trying to cover status bar with my own view and to do that I calculcate frame for my view by doing something like that (also after rotation):
UIScreen *screen = [UIScreen mainScreen];
CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
For iOS8+ (because since ios8 UIScreen is orientantion-dependant):
CGRect frame = [screen.coordinateSpace convertRect:statusBarFrame toCoordinateSpace:screen.fixedCoordinateSpace];
[self setFrame:frame];
For iOS7:
[self setFrame:statusBarFrame];
It works just fine for iOS8 and below but when building my app with Xcode 7 beta 4 and iOS 9 SDK something is wrong when starting app in landscape or upsidedown (it works fine if app starts in portrait) ...
ie. when I start the app while Upsidedown the custom uiwindow which should cover status bar will always end up at the bottom side of screen, any ideas what might be wrong?
Without additional information, the best solution would be to simply hide the status bar. If you still want the "status bar" for animation purposes or some other reason, the typical solution in this scenario is to simulate it by calling
- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates on UIScreen
and use the returned view as the background for whatever view/window you're presenting. As of iOS 9, there's no public method that'll allow you to get the specific behavior you specified.

Corner Radius is not working in iOS7

I want to make my tableViewCell with rounded corner. It is work fine upto iOS6. But in iOS7 rounded corners are not shown.
I used
((UIView*)[self viewWithTag:200]).layer.cornerRadius = 8;
((UIView*)[self viewWithTag:200]).layer.masksToBounds = YES;
Please help me.
Thanks.
Try this category. For me it is working for all iOS.
- (void)setRoundedBorder:(float) radius borderWidth:(float)borderWidth color:(UIColor*)color
{
CALayer * l = [self layer];
[l setMasksToBounds:YES];
[l setCornerRadius:radius];
// You can even add a border
[l setBorderWidth:borderWidth];
[l setBorderColor:[color CGColor]];
}
iOS 7 does not support rounded corners in grouped tables anymore.
iOS 7 is a major overhaul of the whole GUI. Many things have changed, including the appearance of the UITableViews.
You can try to create a custom cell which draws a rounded rect. You have to identifiy the first and last cell in your TableView and only draw the custom View, Background, whatever for those cells.
Here is a link that may help you, although it is targeted for iOS 6:
changing corner radius of uitableview grouped in iOS6

UIImagePickerController live preview orientation wrong

I have a landscape iPad app and I present a UIImagePickerController with sourcetype UIImagePickerControllerSourceTypeCamera to my active view controller modally. However, the live preview is in the wrong orientation and when I take a picture with the wrong orientation, it outputs an image with the correct orientation.
When I rotate the iPad, it fixes the orientation but it makes a thick black bar on one of the sides of the screen depending on the orientation (the camera overlay view isn't cut off by this black bar... only the preview is).
Everything was fine with iOS5, but iOS6 messed up the camera.
How do I fix this live preview orientation bug?
In order to fix the black screen at the bottom of the screen, you have to do a translation and scale, based on the device(iPad or iPhone).
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 25.0);
self.picker.cameraViewTransform = CGAffineTransformScale(translate, 480.0/380.0, 480.0/380.0);
self.picker.cameraOverlayView = self.view;
Try to change the scale values based on the device to make the black bar dissappear.
try this:
dispatch_async(dispatch_get_main_queue(), ^{
[self presentModalViewController:yourUIImagePickerControlle animated:YES];
});

UIImagePicker Front Camera strange behavior on orientation in iPad

Scenario
An app that captures an image from Front camera automatically after 3 sec countdown.
Everything is working as expected.
Code
//in viewDidLoad
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
self.imagePicker.delegate = self;
self.imagePicker.showsCameraControls = NO;
[self.imagePicker.view setFrame:kAppFrame];
[self.view addSubview:self.imagePicker.view];
[self.view bringSubviewToFront:self.imagePicker.view];
}
Problem
I have never told the ipad to use its rear device Still in certain cases the rear camera starts.
Steps to reproduce
Hold the camera in Landscape orientation
Click a photo (Will take you to next screen, pushed in Nav controller)
In the next screen tilt the ipad halfway towards face up orientation
Comeback to camera screen and you see it using rear camera.
This is a very strange behavior I am not even able to understand its cause.
check your code the line self.imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront; define that you open your front vamera of device.
Now your second problem: i am not sure but you have to rotate your image 90 degree. It's may be when you get image it's get in portrait mode.
It looks like I have found the solution.
just moving the entire code to viewWillAppear from viewDidLoad method.
It looked like I was relying a lot on view before it appeared with a certain orientation. Putting the code in viewWillAppear guaranteed me that the view is actually ready with its orientation set. I can now put anything on it.
I know this is not the proper answer but this is what I made out of the scenario. Any modifications/correction are most welcome.

Problems scaling a UIImage to fit a UIButton

I have a set of buttons and different sized images. I want to scale each image in order that it fits the button in the correct aspect ratio. Once I've scaled the image, I set the button's image property to the scaled version.
UIImage *scaledImage = [image scaledForButton:pickerButton];
[pickerButton setImage:scaledImage forState:UIControlStateNormal];
my scaledForButton method is defined in a class extension for UIImage. It looks like this:
- (UIImage *)scaledForButton:(UIButton *)button
{
// Check which dimension (width or height) to pay respect to and
// calculate the scale factor
CGFloat imageRatio = self.size.width / self.size.height;
CGFloat buttonRatio = button.frame.size.width / button.frame.size.height;
CGFloat scaleFactor = (imageRatio > buttonRatio ? self.size.width/button.frame.size.width : self.size.height/button.frame.size.height);
// Create image using scale factor
UIImage *scaledimage = [UIImage imageWithCGImage:[self CGImage]
scale:scaleFactor
orientation:UIImageOrientationUp];
return scaledimage;
}
When I run this on an iPad2 it works fine and the images are scaled correctly. However if I run it on a retina display (both in the simulator and on a device) the image does not scale correctly and is squished into the button.
Any ideas why this would happen on retina only? I've been scratching my head for a couple of days but can't figure it out. They're both running the same iOS and I've checked the scale and ratio outputs, which are always the same, regardless of device. Many thanks.
Found the answer here: UIButton doesn't listen to content mode setting?
If you're setting the .contentMode, it seems you have to set the imageView property of the UIButton, not just the UIButton, then it worked properly.
The problem on iPad 3 was as Herman suggested - the CGImage was still a lot larger than the UIButton, so even though it was scaled down, it still had to be resized to fit the button.