Position UILabel in area relative to screen resolution - objective-c

I am trying to position UILabel relative to screen resolution (iPhone v iPad) so that the UILabel does not interfere with splash screen graphics at start-up. When the app was iPhone only, the label was located properly. Once the app was made universal, the Label interfered with the image on iPad (of course)
I am using the method below, which works fine, but it is not very forward thinking in terms of new devices and/or new screen resolutions.
Can anyone suggest a more efficient way to display the UILabel "Connecting to Server..." within the area circled in red on the attached image at the link below (I do not have auth to post images here yet)?
UILabel *loadingLabel;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 350, self.window.frame.size.width, 20)];
}
else
{
loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 700, self.window.frame.size.width, 20)];
}
loadingLabel.text = #" Connecting to Server...";
loadingLabel.textAlignment = UITextAlignmentCenter;
loadingLabel.textColor = [UIColor whiteColor];
loadingLabel.backgroundColor = [UIColor clearColor];
Splash Screen

You can use the autoresizingMask property to accomplish it.
set your origin.y on the label to be self.window.bounds.size.height - 200 or so. Then set the autoresizingMask to be UIViewAutoresizingMakFlexibleBottomMargin

Related

Best way to place a larger UIView inside smaller one by scaling the larger one to fit

I am grabbing a screen shot (as UIView object) of the view of my next controller and would like to place that screenshot inside a small rectangle in my preceeding controller's view (like a preview). What is the best way to place a large UIView object inside smaller one?
This did not work:
UIView *screenshot = .... // screenshot from the next controller's view
smallViewBox.contentMode = UIViewContentModeScaleAspectFit;
[smallViewBox addSubView:screenshot];
Try setting the bounds of the larger view to match the bounds of the smaller one. I just whipped up a quick example:
UIView *largeView = [[UIView alloc] initWithFrame:CGRectMake(40, 40, 60, 60)];
largeView.backgroundColor = [UIColor redColor];
[self.view addSubview:largeView];
UIView *smallView = [[UIView alloc] initWithFrame:CGRectMake(50,50,40,40)];
smallView.backgroundColor = [UIColor greenColor];
[self.view addSubview:smallView];
largeView.bounds = smallView.bounds;
If you comment out the largeView.bounds = smallView.bounds the green (smaller) box will be the only one visible because it is being drawn over the red box in the controller's view (The two views are siblings in this instance). To make the larger view a subview of the smaller one and restrict it to the smaller one's area you can do this:
UIView *largeView = [[UIView alloc] initWithFrame:CGRectMake(40, 40, 60, 60)];
largeView.backgroundColor = [UIColor redColor];
UIView *smallView = [[UIView alloc] initWithFrame:CGRectMake(50,50,40,40)];
smallView.backgroundColor = [UIColor greenColor];
[self.view addSubview:smallView];
largeView.frame = CGRectMake(0, 0, smallView.bounds.size.width, smallView.bounds.size.height);
[smallView addSubview:largeView];
This will result in the larger view's red color visible - covering the green smaller view's background. In this instance the large view is a child of the small view and occupies its entire area.
You could set a scale transform on it.
screenshot.transform = CGAffineTransformMakeScale(0.5, 0.5);

How to disable Preview screen when using UIImagePickerController Camera

In iOS7 the build-in camera app doesn't go into the preview screen after taking a picture, is there any way that the UIImagePicker can behave like that.
UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
I know another solution is to create a custom camera using the AVFoundation class, but that is beyond my knowledge at this point and I really like the looks of the default camera.
Update
So after some more research I found out I could create my own shutter button and set it as a camera overlay. Here is what I did
UIButton *shutter = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[shutter addTarget:self action:#selector(shootPicture) forControlEvents:UIControlEventTouchUpInside];
[shutter setTitle:#"S" forState:UIControlStateNormal];
shutter.frame = CGRectMake(50, 50, 60, 60);
UIView *layoutView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
layoutView.opaque = NO;
layoutView.backgroundColor = [UIColor clearColor];
[layoutView addSubview:shutter];
For the 'shootPicture' method
- (void) shootPicture
{
[picker takePicture];
[self imagePickerController:self.camCtl didFinishPickingMediaWithInfo:nil];
}
If I just have the picker call 'takePicture' I will still get the preview, so instead I forced the picker to call didFinishPickingMediaWithInfo right after taking picture. The result is I don't see the preview screen HOWEVER I don't see the picture either. I know I put 'nil' in didFinishPickingMediaWithInfo because I don't know what to put in at this point. I also know it took a picture is in cache somewhere but I really have no idea how to get it.
Any idea would be greatly appreciated =)
This solution works for me:
self.pickerController.allowsEditing = false

UITextView renders custom font incorrectly in iOS 7

My app uses a UITextView to input Syriac text (Estrangelo font), but UITextView renders some letters incorrectly like this:
I tested it with a UILabel and a UITextView. UILabel displays it correctly, but UITextView incorrectly displays the top dots and moves them to the bottom (see the above result).
This problem only occurs in iOS 7 and does not occur in iOS 6. Please tell me if there's any way to fix the problem.
This is my test code
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
label.center = CGPointMake(self.view.center.x, self.view.center.y-40);
label.font = [UIFont fontWithName:#"East Syriac Adiabene" size:24];
label.text = #"ܩ̈ ܡ̄ ܬ̇ ܒ̃";
[self.view addSubview:label];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
textView.center = CGPointMake(self.view.center.x, self.view.center.y+40);
textView.font = [UIFont fontWithName:#"East Syriac Adiabene" size:24];
textView.text = #"ܩ̈ ܡ̄ ܬ̇ ܒ̃";
[self.view addSubview:textView];
I debugged this issue a little, and it seems to be a bug in the way NSLayoutManager layouts the text. As other answers pointed out, UITextView is build around TextKit since iOS7, and thus uses NSLayoutManager internally to layout text. UILabel uses Core Text to layout text directly. Both eventually use Core Text to render the glyphs.
You should open a bug report with Apple and post the number so people can duplicate it. The issue has not been fixed so far in iOS7.1 betas.
As a workaround, you can replace UITextView with other Core Text alternative editors, which layout and render directly with Core Text, where the issue does not exist.
I tested SECoreTextView, and it shows the text correctly. It implements a similar API to UITextView but internally uses Core Text.
Here is how it looks after swapping UITextView with SECoreTextView:
SETextView *textView = [[SETextView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
textView.center = CGPointMake(self.view.center.x, self.view.center.y+40);
textView.font = [UIFont fontWithDescriptor:desc size:24];
textView.text = #"ܩ̈ ܡ̄ ܬ̇ ܒ̃";
textView.textColor = [UIColor blackColor];
textView.backgroundColor = [UIColor whiteColor];
textView.editable = YES;
[self.view addSubview:textView];
Some days ago, I had the same problem as yours in iOS 7 (but the font was different).
I set the FONT after setting the TEXT and it worked for me. So for your case:
textView.text = #"ܩ̈ ܡ̄ ܬ̇ ܒ̃"; // Setting the text.
textView.font = [UIFont fontWithName:#"East Syriac Adiabene" size:24]; // Setting the font and it's size.
It may seem silly, but it worked for me.
Also see this question/answer. There are many tips for using custom fonts with UITextView, which may be helpful for you.
EDIT :
iOS 7 also introduced a new selectable property on the UITextView for enabling text selection. So make sure you have done the following:
self.textField.editable = YES;
self.textField.selectable = ON;

Why is there space between the bottom of the imageView and the first TableViewCell in the UITable?

I have a view controller that sets up a UIImageView and a UITableView as follows in viewDidLoad:
// Root UIView
UIView *rootView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
rootView.backgroundColor = [UIColor redColor];
self.view = rootView;
// Image View
NSString *path = [[NSBundle mainBundle] pathForResource:#"test320x180" ofType:#"JPG"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
self.imageView.image = image;
[self.view addSubview:self.imageView];
// Table View
self.tableView = [[UITableView alloc] initWithFrame:rootView.frame style:UITableViewStylePlain];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.contentInset = UIEdgeInsetsMake(image.size.height, 0, 0, 0);
self.tableView.contentOffset = CGPointMake(0, -image.size.height);
self.tableView.backgroundColor = [UIColor clearColor]; // so we can see the image view
[self.tableView reloadData];
[self.view addSubview:self.tableView];
There are 20 pts between the bottom of the image view, which has CGRect: (0, 0, 320, 180), and the first cell of the Table View whose origin is (0, 200), shown in red in the screen shot below.
In reveal, I see that the TableView starts 20 pts below the ImageView, screenshot below. My best guess is that the table view automatically accounts for the status bar but the image view does not.
My intention is to have the image and the first tableview cell flush, but I'm not sure how to guarantee this without adding the magic number 20 to my code.
[[UIScreen mainScreen] applicationFrame] returns a value that includes the entire height of the screen, including the height of the status bar, and always in screen coordinates.
For example on a 3.5-inch iPhone, in both portrait and landscape, you'll get the same application frame:
{ 0,0, 320, 480 } // iPhone 3.5-inch, applicationFrame in both landscape and portrait
When you assign a frame with 0 for origin.y to the image view, then add it via:
[self.view addSubview:self.imageView];
... then the self.imageView's top 20 pixels are hidden beneath the status bar.
Please note, sometimes the status bar's height is doubled, such as by a phone call, or recording audio within an app, or Personal Hotspot/tethering. To survive that, you need the value from:
CGSize size = [[UIApplication sharedApplication] statusBarFrame].size;
This is returned in Screen coordinates, so the returned rect may be:
{ 0,0, 320, 20 } // iPhone 3.5-inch, portrait
{ 0,0, 20, 480 } // iPhone 3.5-inch, landscape
{ 0,0, 320, 40 } // iPhone 3.5-inch, portrait when tethering or other phone call
{ 0,0, 40, 480 } // iPhone
So the quick solution is to use something like the above CGRect size code with the following:
CGFloat height = (size.width < size.height? size.width : size.height);
3.5-inch, landscape when tethering or other phone call
Set the size of the frame of rootView using a CGRect that is
adjusted for the status bar height, so the imageView.frame.origin.y == 0 is
not hidden by the status bar
Set imageView's frame origin y to the status bar height (which means the rootView's content is still overlapped by the status bar).
UITableView is a subclass of UIScrollView so check the contentInset property and also set the UIViewController property automaticallyAdjustsScrollViewInsets is set to NO.
I think automaticallyAdjustsScrollViewInsets applies insets to all scroll views to account for status bars, button bars and navigation bars.
I faced this same issue, while i was working in storyboard where my tableview was having same 20 pixel gap. so what you need to do is:
1.) select your view controller and in attribute inspector deselect "Adjust scroll view inset". (this will remove the gap - that tableview is presuming for navigation bar)
2.) select your tableView and in size inspector set the "section header height" == '1' (this will remove the 20 pixel gap - that tableview is presuming for status bar)
Hope will help you!
Thanks

Navigation bar with prompt - replace with image

How can I make replace a "navigation bar with prompt" with a same size image?
I have seen this in other apps. All I can do is replace the navigation bar alone with no prompt. I want the size with the prompt so that I can put a heighty logo.
Thanks for any resource.
i hope this works for u.
self.navigationItem.prompt = #"";
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"YOUR IMAGE NAME"]];
imageView.frame = CGRectMake(self.view.frame.size.width/2, 0, 10, 10); //change height and width accordingly
[self.navigationController.navigationBar addSubview:imageView];
happy coding :)