How to use Autolayout Programmatically to give vertical space between views? - iphone

In my app i have to layout two views vertically according to each and want to make sure they look similar in iPhone 4&5.
Here's the code :
nextShowView = [[WestwoodNextShowView alloc]initWithFrame:CGRectMake(-5, 322, 290, 70)];
nextShowView.staticLabel.text = #"Next Show";
nextShowView.date.text = #"10/25";
nextShowView.address.text = #"Green Dot Tavern";
nextShowView.cityState.text = #"Detroit, MI";
[_aboutScroll addSubview:nextShowView];
playerView = [[WestwoodMusicPlayerView alloc]initWithFrame:CGRectMake(0, 407, 320, 50)];
[_aboutScroll addSubview:playerView];
NSDictionary * viewsDict = NSDictionaryOfVariableBindings(nextShowView, playerView);
NSArray * constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:[nextShowView]-15-[playerView]-20-|" options:NSLayoutFormatAlignAllLeft metrics:nil views:viewsDict];
[_aboutScroll addConstraints:constraints];
What i tried to do is to give 15 point space between nextShowView and playerView and 20 point space from playerView bottom to the view bottom edge.
Please help !!!!
Thanks,

Autolayout only works if you provide all the constraints that every view needs to specify it completely. You must give not only the vertical separation between nextShowView and playerView, not only the vertical separation between playerView and the superview, but also their heights, and also their horizontal positions and widths.
If your views do not appear where you expect them, you probably have given insufficient constraints. Pause in the debugger and say po [[UIWindow keyWindow] _autolayoutTrace] at the console command line to confirm whether you have ambiguous layout.

Related

ScrollView Detecting Scrolling Everywhere

I have a UIScrollView which is 208pt wide and 280pt tall that contains custom buttons that are 200pt wide and 280 pt tall with 8pt gaps between them. This scrollview has paging enabled but doesn't clip the subviews so that it always snaps to having one button centered but shows the other ones that go off screen. I am trying to make the field in which you can swipe through the buttons take up the full width of the screen, and I am trying to accomplish this with a secondary custom subclass of UIScrollView called PagingView which just has a UIScrollView property and passes all hits on it down to its scrollview. For whatever reason, though, when I try it without the paging view like this:
unsigned height = self.view.frame.size.height;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(60, height - 308, 208, 280)];
scrollView.pagingEnabled = true;
scrollView.clipsToBounds = false;
scrollView.showsHorizontalScrollIndicator = false;
[self.view addSubview:scrollView];
It works, albeit with the field I can interact with the scrollview limited to its frame. However, when I try it with the scrollview:
unsigned height = self.view.frame.size.height;
pagingView = [[PagingView alloc] initWithFrame:CGRectMake(0, height - 308, 320, 280)];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(60, height - 308, 208, 280)];
scrollView.pagingEnabled = true;
scrollView.clipsToBounds = false;
scrollView.showsHorizontalScrollIndicator = false;
pagingView.scrollView = scrollView;
[self.view addSubview:pagingView];
[self.view addSubview:scrollView];
It works, but I am able to swipe anywhere on the screen to move through the scrollview. How do I remedy this?
Not clear what you are doing here. You are adding two scroll views to self.view but actually you need only one.
The easiest way with least code is to use one plain UIScrollView, present your buttons as subviews of correctly sized UIViews (you need the gaps around the buttons), and enable paging for the scroll view. Done.
Please note that the measurements you describe do not work out. The scroll view width is 208, the button 200, so the wrapper view should have width 208 and the origin.x of the button should be 4. The scroll view height is 280, the button as well, so there is no vertical margin: wrapper height also 280, button origin.y is 0.

iOS - trying to rotate a text label and the label disappears

I'm trying to rotate a label on my view 90 degrees. I've tried the two following ways to do it and the label just disappears from the screen. I triple checked that the properties are properly attached. Any thoughts?
attempt one:
// rotating labels 90 degrees
self.labelCloseScroll.transform = CGAffineTransformMakeRotation (3.14/2);
attempt two:
CGAffineTransform rotate = CGAffineTransformMakeRotation(3.14/2);
rotate = CGAffineTransformScale(rotate, 1, 1);
[self.labelCloseScroll setTransform:rotate];
I am not 100% sure if it works or not but why are you not using M_PI_2. It's just simple thought that you are assuming Value of Pi to be 3.14 but the exact value is 3.14159...
I did it like this and it worked fine :
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 70)];
lbl.text = #"New";
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
lbl.highlightedTextColor = [UIColor blackColor];
lbl.font = [UIFont systemFontOfSize:12];
lbl.transform = CGAffineTransformMakeRotation(M_PI_2);
[self.view addSubview:lbl];
You can also check Answers from these Questions :
How to Render a rotated UIlabel
Rotating UILabel at its center
Hope it will be helpful for you.
It may simply be that the view's bounds have become too small for the text. When the text can't be fully displayed in label view in iOS, it simply disappears, rather than remaining on show. Perhaps it's a deliberate Apple policy to prevent apps shipping with clipped text and forcing the dev to fix ;)
It sounds very much as though this is what is happening. You have said the text gets smaller as you rotate it, which indicates you have the shrink text to fit property set on the label view. This will shrink the text as the constraining view reduces in size. But the text will only shrink so much before it disappears.
If the label view itself would seem to be big enough, also be sure to check the bounds of each parent view the label is contained in, up through the view hierarchy.

NSLayoutConstraints in interface builder for 5 labels in a UIview

I think I have a fundamental issue with these new constraints.
Imagine you put 5 labels all going from top to bottom in a UIView. The top label is equally spaced between the top of the view and the next label underneath it. Then each label is also equally spaced.
What I would like to happen is that when the UIView height is made less or more the space between each label changes equally?
What should I be aiming for. Should I make the 3rd label vertically centred in the view and then add constraints to the other labels from the centre or should 1 and 2 be constrained to the top of the view, 3 centre and 4 and 5 to the bottom?
Thanks in advance for all your suggestions.
Edit:
After the below answer was added I have played around with the constraints in interface builder and whilst I am not 100% sure I have answered it I think I have it working.
Between all labels I have a certain distance. But I also have a certain minimum distance like following apples HIG I think it is 15 between labels. So mine are at 30 and I set a constraint of greater than or equal to 15. Now iOS does all the moving around from 30 to 15 for me when the superview changes.
I originally did this with buttons, and the only way I could make this work so that it would automatically adjust to the view frame size (on rotation) was to add labels (with no text, so they're invisible) between each button, and between the containing view and the first and last button. The buttons all had an intrinsic size and the labels did not, so they were free to change their heights to fill the space. I modified the code to use all labels, with the "b" labels being the ones with text.
-(void)viewDidLoad {
[super viewDidLoad];
NSMutableDictionary *viewsDict = [NSMutableDictionary dictionary];
for (int i=1; i<5; i++) {
UILabel *b = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
b.text = #"This is my label";
[b setTranslatesAutoresizingMaskIntoConstraints:NO];
[viewsDict setObject:b forKey:[NSString stringWithFormat:#"b%d",i]];
}
for (int i=1; i<6; i++) { // these are the spacer labels
UILabel *l = [[UILabel alloc ]init];
[l setTranslatesAutoresizingMaskIntoConstraints:NO];
[viewsDict setObject:l forKey:[NSString stringWithFormat:#"l%d",i]];
}
for (id obj in viewsDict.allKeys)
[self.view addSubview:viewsDict[obj]];
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[l1][b1][l2(==l1)][b2][l3(==l1)][b3][l4(==l1)][b4][l5(==l1)]|"
options:NSLayoutFormatAlignAllLeading
metrics:nil
views:viewsDict];
NSArray *constraints2 = [NSLayoutConstraint constraintsWithVisualFormat:#"|-[b1]"
options:0
metrics:nil
views:viewsDict];
[self.view addConstraints:constraints];
[self.view addConstraints:constraints2];
}
This will produce evenly spaced labels with the standard space to the left edge of the view.

UITextView strange issue in setting frame

The above is what I get on the simulator regardless of the frame I set for the UITextView. The above UITextView is a subview to UIScrollView (the light gray view behind). And this UIScrollView has been presented modally. No matter what frame I set for the UITextView, its height remains as shown above. What could be the problem?
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 540, 620)];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 520, 80)];
textView.contentInset = UIEdgeInsetsZero;
textView.layer.borderWidth = 1.0f;
textView.layer.borderColor = [[UIColor blackColor] CGColor];
[scrollView addSubview:textView];
Thanks!
Noted one more thing. Changing the width of the textview from say the above 520 to 220 does indeed result in a change of width and it shows. But no matter what the value of height is, the textview keeps displaying with the same height as in the above image.
your UITextView size is dependent on the Height and width of UIScrollView first increase the size of UIScrollView size than increase th eUITextView size.
I'm getting the same issue and when I try to po the frame height it give me:
property 'frame' not found on object of type 'UITextView *'
This is after installing the new ios6 dev kit... so maybe that's it...
Yup that's it. If you go to file inspector in your xib or storyboard and untick "use autolayout" then that will help.. But's it's screwed everything, so I'm just going to make the damned uitextview programatically!

Manual iPhone rotation incorrectly positioned

I've managed to manually detect rotation and set my own rotation effects between two UIViewControllers. When in the first, rotating to landscape prompts the second to be pushed on with a manual animation. The status bar moves to the correct orientation. However, the view loaded by the second UIViewController is not in the position I expected it. Instead there is margin on the left where the status bar previously was and margin/space at the bottom that I was expecting to be filled by the view.
(Click to enlarge. The orange box is simply to reference where 0,0 is in the rotated UIView, CustomView)
The code I'm using in my view controller to do the rotation is:
-(void)loadView {
CustomView *customView = [[CustomView alloc] initWithFrame:CGRectMake(0, 20, 480, 300)];
self.view = customView;
CGAffineTransform rotate = CGAffineTransformMakeRotation(degreesToRadian(90));
[self.view setTransform:rotate];
[customView release];
}
Any help would be much appreciated!
EDIT Managed to solve after a variety of trial and error approaches - answer provided below. Perhaps there's a more elegant/obvious solution though - if so feel free to provide!
try these on the viewcontrollers. Solved the 20 pixel(status bar height) clip issue when I was getting fullscreen subviews to show up.
[viewcontroller1.view setCenter:CGPointMake(viewcontroller1.view.center.x, viewcontroller1.view.center.y-20)];
and/or
[viewcontroller2.view setCenter:CGPointMake(viewcontroller2.view.center.x-20, viewcontroller2.view.center.y)];
I managed to solve this by setting the View's bounds after the transformation:
self.view.bounds = CGRectMake(20, -20, 480, 300);