I am creating a datepicker view and a toolbar for my iPad app, but I don't know how to set its boundaries. Right now I have this:
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44);
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216);
I think this was designed for the iPhone. Now, in my iPad, how do I set the bounds to make it fit across the entire screen, rather than being cut off? I also need to make this work in portrait and landscape. When I change the dimensions from 320 to 768, the picker is cut off in landscape mode... Thanks!
Related
I am trying to enlarge the size of an UIImageView which is inside a view.
Now the problem is I am showing this view with animation and it's becoming larger from small in size however the image is not enlarging. To make this UIImageView larger I am writing the same code which I wrote for view; that is
self.alertView.frame = CGRectMake(30, 100, 235, 190);
self.alertImgView.frame = CGRectMake(20, 20, 150, 150);
and also i want to show this image view in between of this view that is not happening either. Please suggest
Have you tried this :-
[self.alertImgView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight)];
I have a custom tab bar that was optimized for the iphone 5.
UINavigationBar *myBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
[self.view addSubview:myBar];
When it on the iphone 6, part of it is cut off of course because it only goes to 320 pixels.
How do i fix this? Is there a way to check which iphone its being ran on, and then run the pixel specified code? I plan on putting a background image on this navbar later so it must be centered.
In general it is a bad idea to hard code dimensions like this. This will break if you rotate, or if viewed on a screen with a different size than the original iPhone screen.
Many of these controls have a default size that you can use to your advantage. Instead of handing it a frame, consider just modifying the frame it gives you:
UINavigationBar *myBar = [[UINavigationBar alloc] init];
CGRect navBarFrame = myBar.frame;
navBarFrame.size.height // returns the right size for the current OS
navBarFrame.size.width = self.view.frame.size.width;
myBar.frame = navBarFrame;
This type of defensive coding is helpful in keeping your app laid out properly under many conditions, including when you embed this control into a parent view controller, or viewed on tomorrows larger-screened iOS devices.
All this said, are you sure you don't want a UIToolbar? Typically you don't ever create your own UINavigationBar, as that class is just used in UINavigationController for you.
UINavigationBar *myBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 50)];
[self.view addSubview:myBar];
may be this help you.
I have a subview and I have been customizing its size by using the frame property and setting its value to the CGRectMake function's parameter values.
I have slowly but surely been changing the CGRectMake parameters and re-running the app to get the subview to the correct position on the screen but I know there has to be an easier way.
Here is what I am currently doing:
UIImageView *halfView = [[UIImageView alloc]initWithImage:image];
[self.view addSubview:halfView];
halfView.frame = CGRectMake(0, 0, 320, 270);
Is there a way that I can stop having to manually enter those 4 parameters into CGRectMake, and just set it to the top 50% of the screen?
Here is what I want the subview to look like on the iphone's screen:
halfView.frame = CGRectMake(0, 0, 320, self.view.bounds.size.height/2);
you just take the height and divide it by two
You should also probably change 320 to self.view.bounds.size.width
I suggest reading this post to really get a grasp of UIViews and working with them:
UIView frame, bounds and center
I have a UITextView that should consume the height of the screen, minus the height of the nav bar and keyboard (as when the view loads the keyboard appears). Obviously on a 4 inch device this means the text view should be slightly taller. Is it possible in Interface Builder to make the height device dependent?
If not, can I do it in code? Is it possible to do without some Auto Layout constraints?
It would be very easy to do it by using Auto Layout constraints. Is there any special reason for not using it?
You need to do this check in your code, You can use below Macor to check the device:
#define IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE
then:
if (IS_IPHONE5 )
{
//textView.frame = ..
}
else
{
//textView.frame = ..
}
And yes you can do it using a couple of Auto Layout constraints which is the best practice to do that, check this SO question:
iPhone 4 & 5 Autoresize Vertical Space
you can use struts and springs. but auto layout is the new hotness.
iOS AutoLayout vs Springs & Struts
Going off null's answer:
Im assuming you are placing this UITextView inside a UIViewController?
You can set the frame for the UITextView at runtime.
In viewDidLoad:
-(void)viewDidLoad {
UITextView *textView = [[UITextView alloc] init];
//216 is the height of the keyboard, 20 is the height of the status bar
if (isiPhone5) {
CGRect frame = CGRectMake(0, self.navigationBar.frame.size.height + 20, self.frame.size.width, 568-216);
[textView setFrame:frame];
}
else {
CGRect frame = CGRectMake(0, self.navigationBar.frame.size.height + 20, self.frame.size.width, 480-216);
[textView setFrame:frame];
}
}
This question already has answers here:
How to develop or migrate apps for iPhone 5 screen resolution?
(30 answers)
Closed 8 years ago.
How do I resize a text view so that it expands when displayed in the new 4" screens that it isn't shorter? I'm using AutoLayout. Also, I calculated that since the iOS keyboard takes up 216 points on both devices, and since the Navigation bar takes up 44 points on both devices, that the remaining view would be 200 points on a 320x460 screen. But how do I set this relative to the iPhone 5 screen? Any help would be appreciated. I've provided a screenshot to illustrate what I mean to better understand my issue.
Normal Screen (Works great!):
iPhone 5 Screen (Not so great!):
Try do like this:
self.view.autoresizesSubviews = YES;
self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
Just ended up disabling autolayout for that specific view controller, and set my own CGRect.
CGRect largeScreen = CGRectMake(0, 1, 640, 290);
CGRect screenBounds = [[UIScreen mainScreen]bounds];
if (screenBounds.size.height==568)
{
[self.htmlText setTranslatesAutoresizingMaskIntoConstraints:YES];
htmlText.frame = largeScreen;
}
It is quite simple calculate height of textfield like this
CGFloat TextFieldHeight = self.view.frame.size.height - keyboardHeight- statusbarheight - navigationBarHeight;
Then assign this height to your textfield.
CGRect TextFieldNewFrame = textField.frame;
TextFieldNewFrame.size,height = TextFieldHeight;
textField.frame = TextFieldNewFrame;