How to I resize an ImageView in the ViewDidLoad? - ios

I have an imageView that is resized in two different parts of my code. This works great in my textFieldDoneEditing. However, when I use the same code in my viewDidLoad, I get a different sized view. Is there a difference with doing this in the that method?
Here's the code:
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGRect frame = CGRectMake(0,0, screenWidth, screenWidth);
PuzzleImage.frame = frame;
Thank you

If your ViewController is landscape mode, viewDidLoad always gets main screen's bounds in portrait mode.
So, solution is setFrame in viewWillAppear to get right bounds.

Related

UIScreen mainScreen bounds give wrong size

Hi i am initiate a rootViewController programatically (portal only no landscape]
this is the code use
CGRect appFrame = [[UIScreen mainScreen] bounds];
But it does not give correct size. (cut down at bottom screen)
Only this code give correct
CGRect appFrame = [UIScreen mainScreen].applicationFrame
But this code will give warning of depreciated (applicationFrame is depreciated)
(My simulator is IOS 9.3)
How to fix the problem? any help is much appreciate! thanks!
here is screenshoot when using [UIscreen mainscreen].bounds
and here is [UIscreen mainscreen].applicationFrame
[[UIScreen mainScreen] bounds] gives the correct size of the entire window, but it includes the size of the status bar. If you want the size of the status bar ( you can subtract its height), you should use [[UIApplication sharedApplication] statusBarFrame] to get the frame of the status bar. Navigation bars and tab bars generally have a height of 44. So, use CGRectMake() if you need a rectangle for your view:
CGRect frame_screen = [[UIScreen mainScreen] bounds];
CGRect frame_view = CGRectMake(0,0,frame_screen.size.width,frame_screen.size.height - [[UIApplication sharedApplication] statusBarFrame].size.height);
Notice that the last argument of CGRectMake is the height, usually you can minus 44 for a tab bar or navigation bar.
EDIT: Try to log [UIScreen mainScreen].applicationFrame and [[UIScreen mainScreen] bounds] to console and see what the difference is between them. Something like
CGRect frame_screen = [[UIScreen mainScreen] bounds];
NSLog(#"x: %f, y: %f, width: %f, height: %f",frame_screen.origin.x,frame_screen.origin.y,frame_screen.size.width,frame_screen.size.height);
CGRect frame_application = [UIScreen mainScreen].applicationFrame
NSLog(#"x: %f, y: %f, width: %f, height: %f",frame_application.origin.x,frame_application.origin.y,frame_application.size.width,frame_application.size.height);
Then use that information to make [[UIScreen mainScreen] bounds frame how you need it.
you can do the following.
CGRect rect = [UIScreen mainScreen].bounds;
CGRect screenFrame = CGRectMake(0, [[UIApplication sharedApplication] statusBarFrame].size.height, rect.size.width, rect.size.height - [[UIApplication sharedApplication] statusBarFrame].size.height);
Now you can use this screenFrame, I hope this will resolve your problem.
Make sure you have added all resolution splash image on yous's application Images.xcassets
`
Please take a look on Screen shot i attached
`
CGRect appFrame = [[UIScreen mainScreen] bounds]; //This is the
correct way to get your screen size

Manually rotate and resize UILabel on orientation change

In my previous question I shared my problem with the black background appearing in my app on orientation change: Black background when view rotates on orientation change
I did not manage to solve the problem by following any of the advices I got and I have the feeling that the only way I can avoid the black background is by manually rotating my subviews on orientation change?
One of my subviews is a UILabel which is supposed to cover the entire screen. The rotation is going pretty well using a line of code similar to this one:
myLabel.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(isLandscape ? 90 : 0));
My problem is to make the UILabel adjust to take up the entire screen in landscape mode as well. I have tried to switch height and width of its bounds, but nothing happens.
Any suggestions will be greatly appreciated.
Here are some more code details:
[UIView animateWithDuration:0.5
animations:^{
myLabel.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(isLandscape ? 90 : 0));
}
completion:^(BOOL finished) {
myLabel.bounds = CGRectMake(0, 0, [self screenWidth], [self screenHeight]);
CGFloat fontSize = ((isLandscape ? 0.9649 : 0.9375) * [self screenWidth]) / 2.74;
[myLabel setFont:[UIFont fontWithName:FontName size:fontSize]];
}
];
where
- (CGFloat) screenWidth {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
return isLandscape ? MAX(screenSize.width, screenSize.height) : MIN(screenSize.width, screenSize.height);
}
- (CGFloat) screenHeight {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
return isLandscape ? MIN(screenSize.width, screenSize.height) : MAX(screenSize.width, screenSize.height);
}
Perhaps you are calling sizeToFit on the label somewhere?
Try instead to set:
myLabel.adjustsFontSizeToFitWidth = YES;
myLabel.minimumFontScale = 0.1;
This will adjust your labels font size to fit the width of label.
I found out that I was able to adjust the label size once I unchecked the auto layout checkbox.

UIView's frame rendering larger than frame

I am trying to add a subview to a UIScrollView. First I instantiate the view controller from the storyboard, then set the view's frame to the application bounds. When I add the view to the UIScrollView, it is clearly larger than it's supposed to be.
CGRect mainFrame = CGRectMake(0, topButtonHeight, screenWidth, screenHeight);
feelingVC = (FeelingViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"feelingVC"];
feelingVC.delegate = self;
feelingView = feelingVC.view;
[feelingView setFrame:mainFrame];
[self.scrollView addSubview:feelingView];
I can tell because its background color is extending past where it should be. "Debug View Hierarchy" mode in XCode also confirms this. But, if I inspect the view's frame, it prints out what it should be, not what it actually is.
A view of the same size, but generated completely programatically, works as it should:
mainView = [[UIView alloc] initWithFrame:mainFrame];
mainView.backgroundColor = [UIColor blackColor];
[self.scrollView addSubview:mainView];
I'm not sure what could be causing this issue - I've instantiated other views from their view controllers, also via the storyboard, and set their frames without any problems.
EDIT: This is being called from viewDidLoad of the view controller containing the UIScrollView. screenWidth & screenHeight are calculated as such (they are instance variables):
screenWidth = [UIScreen mainScreen].applicationFrame.size.width;
screenHeight = [UIScreen mainScreen].applicationFrame.size.height;
Try to set the view frame in viewWillAppear method.
viewDidLoad is not a good place to set frames (because it called only once when view getting loaded.), the UI components are not properly configured by the system yet.
Also, prefer to use:
screenWidth = [UIScreen mainScreen].bounds.size.width;
screenHeight = [UIScreen mainScreen].bounds.size.height;
instead of
screenWidth = [UIScreen mainScreen].applicationFrame.size.width;
screenHeight = [UIScreen mainScreen].applicationFrame.size.height;
since bounds has correct positional information taking into consideration the device's orientation in this case.
Or you can just do this, after initializing the mainView inside viewDidLoad: Method
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
mainView.frame = [UIScreen mainScreen].bounds;
}
you can also add this to reconfigure the view frame whenever the subviews are updated:
- (void)viewWillLayoutSubviews { //or viewDidLayoutSubviews
[super viewWillLayoutSubviews];
mainView.frame = [UIScreen mainScreen].bounds;
}

Autolayout UITextView inside UIScrollView size not correct

So I know there are already a lot of questions about this subject, but I haven't found one that has solved my problem (or perhaps I don't understand the answer).
Alright, so I have set up a scrollview that has an UIView in it, containing an image view at the top (a gradient view which you can ignore), and a textview under it. The textview has to expand with whatever is put inside of it without scrolling (hence why it's in the scrollView).
I have in code:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self setupGradientView];
[self resize];
}
- (void) resize {
CGFloat maxWidth = [[UIScreen mainScreen] bounds].size.width;
CGRect newFrame;
// reset width for scrollview
newFrame = imageView.frame;
newFrame.size = CGSizeMake(maxWidth, imageView.frame.size.height);
imageView.frame = newFrame;
gradientView.frame = newFrame;
newFrame = textView.frame;
newFrame.size = CGSizeMake(maxWidth, textView.frame.size.height);
//textView.frame = newFrame;
newFrame = dummyView.frame;
newFrame.size = CGSizeMake(maxWidth, dummyView.frame.size.height);
dummyView.frame = newFrame;
// resize height
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, CGFLOAT_MAX)];
newFrame = textView.frame;
newFrame.size = CGSizeMake(fixedWidth, newSize.height);
//textView.frame = newFrame;
self.textViewHeightConstraint.constant = newSize.height;
CGFloat dummyViewHeight = textView.frame.origin.y + textView.frame.size.height;
dummyView.frame = CGRectMake(dummyView.frame.origin.x, dummyView.frame.origin.y, dummyView.frame.size.width, dummyViewHeight);
[scrollView setContentSize:dummyView.frame.size];
}
Unfortunately, the content size seems to be off by what I think may be 16pts, resulting in a view that looks like this with the text cut off (don't mind the unicode mess in the text):
Here's a picture of what the view looks like in IB: https://i.imgur.com/hxzzUg2.png
Here's what the constraint hierarchy looks like: https://i.imgur.com/rUepwa2.png
Any help would be greatly appreciated!
EDIT: If I comment out the entire code in the resize function, I get the same result. It would appear that auto-layout is resizing all of this after the function is called...but I thought auto-layout was completed when viewDidLayoutSubviews was called?
Edit 2: I have tried and found that adjusting the frame of the textView even n viewDidAppear has no effect. (I can edit things such as the background color)
What I ended up having to do was setting equal width and equal height to the view (not the scrollview), and then manually changing the height constraint in viewDidLayoutSubviews, along with changing the frame of the textView and the content size of the scrollView.

How to drawrect paintcode content centre horizontal on landscape iOS on retina iPad?

I'm getting in a bit of a muddle trying to draw some paintcode graphics code in the middle of the screen horizontally.
I'm really not sure if scale or landscape orientation affects this.
I've created a UIView subclass and addSubview in viewDidLoad like so...
MenuTitleView *fancyView = [[MenuTitleView alloc] initWithFrame:self.view.bounds];
[[self view] addSubview:fancyView];
Rather than the size PaintCode gave me...
CGRect textRect = CGRectMake(0, 0, 418, 129);
I'm trying to determine the screen / view width and the size of the canvas width.
I've tried various things without success.
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGFloat w = screenRect.size.width;
CGFloat width = 500;
CGFloat height = 200;
CGFloat x = (w - width) * 0.5f;
CGFloat y = 20;
CGRect textRect = CGRectMake(x, y, width, height);
Scale is not an issue, because Quartz and UIKit now work in 'points' rather than in pixels, so that's already accounted for. However orientation is not, [UIScreen mainScreen] will return the same rect either for applicationFrame or bounds etc, it's oblivious to orientation.
I like to put a solution in a category on UIView or UIViewController, because you'll reuse it plenty. something like this...
-(CGRect )workingCanvas{
CGRect screenRect = [UIScreen mainScreen].bounds;
BOOL landscape = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
CGRect result = CGRectZero;
CGFloat lesserDimension = (screenRect.size.width < screenRect.size.height) ? screenRect.size.width : screenRect.size.height;
CGFloat greaterDimension = (screenRect.size.width > screenRect.size.height) ? screenRect.size.width : screenRect.size.height;
result.size.width = (landscape) ? greaterDimension : lesserDimension;
result.size.height = (landscape) ? lesserDimension : greaterDimension;
if ([[UIApplication sharedApplication]isStatusBarVisible]){
result.size.height -= [[UIApplication sharedApplication]statusBarFrame].size.height;
}
return result;
}
if you want to position your view just in the middle of the screen try this
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGFloat width = 500;
CGFloat height = 200;
CGFloat x = CGRectGetMidX(screenRect);
CGFloat y = 20;
CGRect textRect = CGRectMake(x, y, width, height);
already tested is working
Good Luck!!

Resources