I know it's may be a stupid question to ask. But I am designing a single-view application in xcode 3 , ios 5. Programatically adding a subview and imageview to take up the whole screen. But it shows a small gap at the bottom of my view on the screen. Not sure why. Here is the code. any suggestion ?
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
homeView =[[UIView alloc] initWithFrame:self.view.frame];
UIImageView *backImage= [[UIImageView alloc] initWithFrame:self.view.frame];
backImage.image=[UIImage imageNamed:#"bg.png"];
[homeView addSubview:backImage];
[self.view addSubview:homeView];
The gap was due to some transparent pixels in the image i was using. Nothing was wrong with the code. It has been resolved.
Thanks
Related
I have been working with constraints in Swift for iOS, and the documentation does a good job of explaining things.
However, one thing that has recently confused me is the difference between view.topAnchor and view.safeAreaLayoutGuide.topAnchor.
When I have set constraints programmatically in my iOS app, I have found that I can use these totally interchangeably, so I can't tell what the difference between the two is.
I checked out the documentation at developer.apple.com, and I haven't found anything explaining the differences.
Is there any difference between the two properties?
The main difference is with devices that has top notch screens as well as home button like iphone 11 inside your view other than physical button like iphone 8 and older. also as it says it keeps you in the safe area even while device in landscape rotation, however some designs require using top anchor.
UIView.topAnchor represents the top edge of the view's frame.
UIView.safeLayoutGuide is a rectangle area representing the view's safe area, the portion of the view that is unobscured by bars and other content.
UIView.safeLayoutGuide.topAnchor is the top edge of UIView.safeLayoutGuide.
Example:
Try create an empty storyboard project in Xcode, modify "ViewController.m" with the code below. This code create red topView and bottomView that is inside the parent view, but outside of the view's safeArea.
#import "ViewController.h"
#interface ViewController()
#property UIView* topView;
#property UIView* bottomView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
self.topView = [[UIView alloc] init];
[self.topView setTranslatesAutoresizingMaskIntoConstraints:NO];
self.topView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.topView];
[NSLayoutConstraint activateConstraints:#[
[self.topView.topAnchor constraintEqualToAnchor:self.view.topAnchor],
[self.topView.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor],
[self.topView.widthAnchor constraintEqualToAnchor:self.view.widthAnchor],
]];
self.bottomView = [[UIView alloc] init];
[self.bottomView setTranslatesAutoresizingMaskIntoConstraints:NO];
self.bottomView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.bottomView];
[NSLayoutConstraint activateConstraints:#[
[self.bottomView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor],
[self.bottomView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor],
[self.bottomView.widthAnchor constraintEqualToAnchor:self.view.widthAnchor]
]];
}
Some screenshots of the exmaple: (Please ignore the button)
I'm trying to implement a ScrollView, but when I put a View inside, the View looks good in the preview but it's not the same when I run the application:
http://i.stack.imgur.com/ZZfEg.jpg
And I don't know how to resize a text of the width of the view.
Without the ScrollView I can do a good structured view but when I use ScrollView I cannot get a good view, how to do ?
Code here: http://pastebin.com/LN5FySku
I think this task is very easy if you use code rather than storyboard.
Use this code..
You can do this in Storyboard as well but i prefer using code for such tasks. Hope that helped.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 520)]; // little less than 568 to accommodate the tabBar
[scrollView setContentSize:CGSizeMake(320, 1500)];
UIView *largeView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 320, 100)];
[scrollView addSubview:largeView];
[self.view addSubview:scrollView];
for further more information check this link.
My problem is solved, the reasons:
UIImageView: I think my view have lost the images when i did a copy past of the view, so click on your UIImageView and set the image field.
UILabel: For the cropped text you have to create the Equal Width Constraint between the ContentView and the View, select the ContentView on the view hierarchy and Control + Drag to the View – you should get a pop-up that gives you the “Equal Widths” option.
I have written some code which applies and image to a view for a UIViewController. The code is supposed to be iPhone screensize independent in as far as the difference in height between the iPhone 4 and 5.
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.view.clipsToBounds = YES;
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:#"myimage.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIView *imageView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
imageView.backgroundColor = [UIColor colorWithPatternImage:image];
[self.view addSubview: imageView];
image = nil;
imageView = nil;
I found that when I add this code to the viewDidLoad method, the code failed to detect the different window size. However when I place it in viewWillAppear, the code does correct work with both screen sizes. I don't understand why.
Does any one know why this would happen ? I would like to understand it.
thanks
This happens because at the time the view is loaded, its' content hasn't necessarily been laid out and the size isn't known. This is especially true when using the autolayout system. The basic steps are,
The view is loaded
The view is laid out by the system using the constraints you give in the storyboard or code
The view appears
So the most appropriate place to put this appears to be viewDidLayoutSubviews. At that point the view and its subviews have been laid out, and the sizes are there. But putting it in viewWillAppear (or viewDidAppear, for that matter) will work, albeit will be less correct.
viewDidLoad method is called when the view controller's root view just has been created. At this point it is not added to the window hierarchy and auto layout has not been done. That is why you see this behavior.
This being said why draw the image in the graphic context? Just use UIImageView to display the image.
What you are doing is almost correct. As others have stated, in viewDidLoad the final size of the view controller's view is not set yet. The proper solution is to set the subview's autoresizingMask properly.
UIView *imageView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
imageView.backgroundColor = [UIColor colorWithPatternImage:image];
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview: imageView];
This assumes you want the image view to fill the view controller's view.
Another solution is to update the frames of the subviews in the viewWillLayoutSubviews method.
Hi I was learning how to use autoresizing and scroll view also and came up with this solution which works, just your feedback would be welcome on my approach if it is ok how it could be improved etc.
I just created a test view controller whose view looks like this.
The grey area is a UIView called mainView and it is subview of view controller's view (and it's longer than view controller's view).
When I changed rotation one would see such thing:
You can see the calculator does not fit on the screen anymore.
So I decided to have a UIScrollView already from start. Add mainView as a subview to it.
Final thing that was remaining, I wanted the frame of the ScrollView to change also when device rotates, so that I could get such effect:
(you see the scroll views width is same as phones).
This is how I implemented all this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[self.view bounds]];
[[self view] addSubview:scrollView];
[scrollView addSubview:self.mainView];
[scrollView setContentSize:[mainView bounds].size];
[scrollView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleBottomMargin];
}
Any comments and feedback?
You can use the didRotate methods to position your buttons how you want in the different orientations. There is a good tutorial here: http://www.youtube.com/watch?v=f3yb24f8O1Y&list=PL53038489615793F7
I have a tab bar with 3 buttons, each of which loads a different controller and hence a different view.
I would like to place a UIView right behind my tab bar so that it is visible on all 3 different sub-controllers.
How can I achieve that?
It's pretty easy (this is using a storyboard) :
• Create a subclass of UITabBarController (I'll call it "TabViewController").
• In your storyboard, select your UITabBarViewController, and give it the class `TabViewController (on the right bar, 3rd section, custom class).
• In your TabViewController.m file, use this code :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIView *theView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
theView.backgroundColor = [UIColor redColor];
[self.view addSubview:theView];
[self.view bringSubviewToFront:self.tabBar];
}
You can do whatever you want with theView before you add it to self.view, here I just create a 50x50 red square at the position (50, 50). The view will stay on top of everything else !
Run & have fun !
add that green banner on Window in appDelegate.
[self.window addSubview:greenBannerView];