Handling view resizing programatically heights, widths and margins - ios

I am trying to setup a view programatically. Through preference I prefer to programme the views as opposed to using Interface Builder, I feel I have a better control for some reason...
I am setting up a view with two subviews and a button. What I am trying to acheive is the same view when the orientation changes. Initially I thought I needed to calculate the screen size then calculate some divisions to work out the changes, but it appears I can handle on UIViewAutoresizing***
The issue I experience is with the top margin. Here is my code for the subviews.
// Create sub view for Logo
UIView *logoView =[[UIView alloc] initWithFrame:CGRectMake(0,0,320,280)];
[logoView setBackgroundColor:[UIColor blueColor]];
logoView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
// Create sub view
UIView *buttonView =[[UIView alloc] initWithFrame:CGRectMake(0, logoView.bounds.size.height, 320,200)];
[buttonView setBackgroundColor:[UIColor redColor]];
buttonView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin;
Here is a picture of portrait and landscape where you can see the issue, with the 'white' space.

You either want the two views (red and blue) to end up with a proportionate amount of space after the rotation:
logoView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin;
buttonView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin;
Or you want the red view to end up the same size as it started out, and the blue view to adjust to make room for it:
logoView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
buttonView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;

use autolayouts . U dont have to deal with any of these issues ...

Related

Make part of UIView visible

I want to add a "shadow" image on my view, but, i want part of my view to still be "visible". You better understand what i want to do, when look on screenshot:
I can add a UIView above my superview, but how could i make specific point "visible"? That actually mean make specific area of a view with different colour or opaque.
For your problem I had a way, try the below example.
1. First storyboard design: I had a UIButton
2. Added black View as a subview
UIView *blackView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
blackView.backgroundColor=[UIColor blackColor];
blackView.alpha=0.6f;
[self.view addSubview:blackView];
now result will be:
3. Add one more UIImageView with frame equal to button frame
UIImageView *imageView=[[UIImageView alloc]init];
imageView.frame=self.button.frame; //getting current UIButton bounds
imageView.image=[UIImage imageNamed:#"add_img.png"];
[self.view addSubview:imageView];
Now it looks same as what you want:
You have to add some views. A main view will be the container of your less-alpha view and of the opaque button.
MainView
|
------- UIView with 0.6 alpha
|
------- UIButton with 1 alpha
Indeed, if you change the alpha of the MainView, all subviews will be affected. Here, UIView with 0.6 will have the same frame as MainView but it will not affect UIButton alpha.
Make a new View, a parent View with clear color background. Add the black view and the button to the parent view and set the black view's alpha to 0.6 or whatever.
Try this:
Make a UIView called buttonBackgroundView, give it a black color and then
[self.view addsubView:buttonBackgroundView];
now make a UIView named plusView (or UIButton if this plus sign is a button) then
[self.view addsubView:plusView];
After that, give the alpha to the buttonBackgroundView
buttonBackgroundView.alpha = 0.6;
Happy coding!

UIScrollView The view change at run

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.

iOS View gets reset on addSubview

I have a storyboard with a square imageView in the middle at: 280,140 (vertical)
When the application starts i am able to move this imageView by pressing buttons. I move the imageView using:
_mainImage.frame = CGRectMake(_mainImage.frame.origin.x + 2, _mainImage.frame.origin.y, _mainImage.frame.size.width, _mainImage.frame.size.height);
while the application is running i keep adding new imageViews to the mainView:
UIImageView *imgView;
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, yCoordinate, 50, 10)];
imgView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:imgView];
each time i call this code the square imageView i have placed in the middle and moved around, gets set back to its starting position. It seems like the mainView gets "reset" each time the addSubview gets called. Is there any way i can add a subview without having the mainView "reset"?
Most likely your view is set up using Autolayout (this is the default for storyboards or nibs) and when you add a new subview, a layout pass is performed which resets your views position back to that defined by its original constraints.
You should move your view by updating its constraints, or turn off Autolayout. There are lots of posts around explaining how to do either of these things.

Screen bounds extends visible area

I'm trying to make a square (50 x 50) UIView in the lower right hand of my main view, but I'm confused about why it's not entirely visible. In this instance, only the tip of it is visible from the bottom. Am I confusing some concept?
CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);
UIView *transparentFloater = [[UIView alloc] initWithFrame:CGRectMake(width - 50.f, height - 50.0f, 50.0f, 50.0f)];
[self.view addSubview:transparentFloater];
Try to add the autoresizing mask:
transparentFloater.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
If I am not wrong, You have created your ViewController with xib and in the attributes of view, you have forgot to mention that you are using a "navigation bar" as "top bar".
Reason
So in ViewDidLoad, view in .xib with a height X is returned. But as you might have loaded the viewController using UINavigationController, after loading the height of view is decreased by 44pixels.
Suggestions :
1) Whenever you know that you are going to have navigation bar on top please mention it in .xib file as well.
For doing so click on the view in xib and select the option called topbar in attributes Inspector of utilities area
2) Call the following api
NSLog(#"In ViewDidAppear %#",NSStringFromCGRect(self.view.bounds));
in
viewDidLoad
viewDidAppear
to track whats happening with the bounds of view
You does not need to do anything just put
transparentFloater.backgroundColor = [UIColor redColor];
because your view (custom) is invisible (because your main view color and custom view color are same (white) ) may be. I tried your code in my demo project. it worked perfectly for me.

iOS UITableView: UIView/custom cell at the bottom like in twitter iPad app

I am creating an iPad app using the master-detail template available in Xcode 4.3. My master tableview is acting as a navigation menu for the detail view and the menu items will be fixed. So I basically don't exactly need the scrolling view, thus I have turned it off.
self.tableView.scrollEnabled = NO;
Now I have a requirement to display a footer like cell aligned at the bottom of master menu just like in Twitter iPad app. The cell should appear at the bottom in landscape as well as portrait modes. Can somebody give me some hints regarding how to implement this?
I read on some blogs about using a UIView and setting it to UITableView.tableFooterView, something like this...
// I'll have to do calculations of frame height/x/y for both orientations
// to make the view appear at bottom - IS THERE A SIMPLER WAY???
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 944, self.tableView.frame.size.width, 60)];
UILabel *logo = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 60)];
logo.text = #"This is the Footer.";
[footerView addSubview:logo];
self.tableView.tableFooterView = footerView;
After looking at the app, I don't think the "footer" is part of the table. It looks more like a small view under the table. So the table is set up so it will stretch vertically but it's height is locked above the bottom view. Maybe it would be better to use a UIViewController and a UIView for you Master View instead of a UITableViewController. Then put your UITableView in the UIView and put your footer below it. Then configure the UIViewController to work with the UITableView as it did before.
Hope this helps.

Resources