make buttons stick to side when view rotates - objective-c

I have a view where I in manually in my code add 2 buttons as subview and place them at a specific location (lower right corner).
How do i make them "stick" to the lower right corner when the view is resized? (on rotation)
Thanks

Did you try to set the autoresizingMask of your buttons ?
button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
It assumes that the autoresizesSubviews property of the button's superview is set to YES (that is the default value)

You should implement method
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
in your UIViewController and manually change the frame of the button, changing the origin. i.e:
CGRect frame = button.frame;
frame.origin.x += 30;
frame.origin.y -= 20;
button.frame = frame;

Related

How to add a scroll view to an ios app

I have a tabbed application that I would like to have a scroll view on. I already have a few text fields and labels on that tab. The problem is, the keyboard hides some of the text fields. How would I add a vertical scroll to prevent that?
You can accomplish what you are looking for without a scrollview.
In you delegate method for you textfield, You can change the frame of the viewController's view.
something like
- (void)textFieldDidBeginEditing:(UITextField *)textField;
{
CGRect newFrame = self.view.frame;
newFrame.origin.y = - 40; // move the view up to the point your textfield is visible
self.view.frame = newFrame;
}
Then in Set it back
- (void)textFieldDidEndEditing:(UITextField *)textField;
{
CGRect newFrame = self.view.frame;
newFrame.origin.y = 0;
self.view.frame = newFrame;
}
Usually I animate this with 0.33 seconds duration.
The solution requires quite a bit of code, but here's the general idea of what you need:
You will need to add the text fields (and everything else for consistency) to a scrollView.
You need to setup the scroll view to have vertical scrolling space only, but set the scrollEnabled to false so that the user can't scroll it manually.
Then you need to listen to UIKeyboardWillShowNotification and UIKeyboardWillHideNotification and manually scroll it up/down as required.

Shifting Window Frame without moving its contents in Objective-C

I have (+) and (-) buttons... The (+) button should make the window longer in the downward direction, and the (-) button should undo what the (+) button did, by making it shorter from the bottom.
The point is, the frame should only be getting stretched from the bottom and most of the UI items should be staying in place - there are buttons on the bottom of the window that should follow the change in window height...
The problem is I'm using this to shift the window and sustain the same position of its contents...
NSRect frame = sender.window.frame;
NSLog(#"\nHeight and width of window frame: (%f,%f).\nThe x and y origin of the window frame: (%f,%f). ", frame.size.height, frame.size.width, frame.origin.x, frame.origin.y);
frame.origin.y -= 22;
frame.size.height += 22;
[sender.window setFrame:frame display:YES animate:NO];
NSRect viewFrame = mainView.frame;
viewFrame.origin.y -= 22;
viewFrame.size.height += 22;
mainView.frame = viewFrame;
But say you click the (+) button once; all the items shift up slightly while the frame gets longer. If you click (+) consecutively after the first press it will function as expected: the frame will move down and its contents will stay in place.
If you then clicked the (-) button once; all the items shift down slightly while the frame gets shortened. Clicking the (-) consecutively after the first press, it will just move the frame as expected.
It seems pressing the button the first time will move all the windows contents, while consecutive presses will function as intended... I'd like to know if I'm missing something about autosizing in the Interface Editor of XCode. Thank you.
Assuming that mainView is the main view in your window, you should not be modifying its frame manually. So long as you have your springs and struts (or constraints) configured properly, it should resize automatically with the window.
[mainWindow disableScreenUpdatesUntilFlush];
[mainWindow disableFlushWindow];
NSRect frame = sender.window.frame;
NSRect viewFrame = mainView.frame;
viewFrame.size.height += 25;
frame.size.height += 25;
frame.origin.y -= 25;
[sender.window setFrame:frame display:YES animate:NO];
mainView.frame = viewFrame;
[mainWindow enableFlushWindow];
This works fine. I couldn't get the NSView and the window linked so I just move them both manually and use disableFlushWindow to make it happen seamlessly at the same time.

Set view position to top right programmatically in cocoa touch

I need to make sure that one view A (size: 200x200) is always aligned to top right corner inside second view B (full screen size). I want to make sure that view A stays in that place regardless of device orientation. Truth is I have no problem with this when using interface builder to position the views but I need to construct this programmatically. I suppose I should use some autoresizing settings, could you tell me which one is supposed to align the view to top right corner of its superview?
UIView parentView //your full screen view
UIView view //the 200x200 view
[parentView addSubview:view];
CGRect frame = view.frame;
//align on top right
CGFloat xPosition = CGRectGetWidth(parentView.frame) - CGRectGetWidth(frame);
frame.origin = CGPointMake(ceil(xPosition), 0.0);
view.frame = frame;
//autoresizing so it stays at top right (flexible left and flexible bottom margin)
view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin
That positions the view on top right and set's the autoresizing mask so it stays at that position.

Cocoa auto-resizable window

How can I make my NSWindow with NSTabView smoothly resize when user clicks a tab? I want it to like "System Preferances" application: window changes its size according to content.
Use NSWindow's setFrame:animated: method. If you want to resize the window down, make sure you decrease the y coordinate of the origin by the same amount you increase the size of the window. To also resize the views in the window, make sure you set up their autoresizing properties correctly.
NSWindow *window;
CGFloat widthChange, heightChange;
NSRect frame = [window frame];
frame.size.width += widthChange;
frame.size.height += heightChange;
frame.origin.y -= heightChange;
[window setFrame:frame animated:YES];

Scrollable UINavigationBar similar to Mobile Safari

My application uses a UINavigationController and the final view (detail view) lets you view an external website within the application using a UIWebView.
I'd like to free up some additional screen real estate when the user is viewing a webpage and wanted to emulate how Safari on iPhone works where their URL bar at the top scrolls up and off the screen when you're viewing content in the UIWebView that's below the fold.
Anyone have ideas on how to achieve this? If I set the navigationBarHidden property and roll my own custom bar at the top and set it and a UIWebView within a UIScrollView then there are scrolling issues in the UIWebView as it doesn't play nicely with other scrollable views.
Based on #Brian suggestion I made this code:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat height = navigationBar.frame.size.height;
CGFloat y = scrollView.bounds.origin.y;
if (y <= 0) {
CGRect frame = navigationBar.frame;
frame.origin.y = 0;
navigationBar.frame = frame;
} else if (tableView.contentSize.height > tableView.frame.size.height) {
CGFloat diff = height - y;
CGRect frame = navigationBar.frame;
frame.origin.y = -y;
navigationBar.frame = frame;
CGFloat origin = 0;
CGFloat h = height; // height of the tableHeaderView
if (diff > 0) {
origin = diff;
h = y;
}
frame = tableView.frame;
frame.origin.y = origin;
frame.size.height = tableView.superview.frame.size.height - origin;
tableView.frame = frame;
CGRect f = CGRectMake(0, 0, tableView.frame.size.width, h);
UILabel* label = [[UILabel alloc] initWithFrame:f];
tableView.tableHeaderView = label;
[label release];
}
}
My code has a UITableView but should work with any scrollable component. If you have other components than the navigationBar and the UIScrollView subclass, you should change the way the height of the scrollable component is calculated. Something like this:
frame.size.height = tableView.superview.frame.size.height - origin - otherComponentsHeight;
I needed to add a dumb tableHeaderView to have the desired behaviour. The problem was that when scrollViewDidScroll: is called the content has an offset, but the apparience in Mobile Safari is that the content is not scrolled until the navigationBar fully disappears. I tried first changing the contentOffset.y to 0, but obviously it didn't work since all the code relies on the scrolling mechanism. So I just added a tableHeaderView whose height is exactly the scrolled offset, so the header is never really seen, and the content appears to not scroll until the navigationBar fully disappears.
If you don't add the dumb tableHeaderView, then the scrollable component appears to scroll behind the navigationBar.
With the tableHeaderView, the scrollable component is actually scrolling (as seen in the scrollbar), but since there is a tableHeaderView whose height is exactly the same than the scrolled offset, the scrollable content appears to not be scrolling until the navigationBar fully disappears:
Have a delegate for the scrolling events in the UIWebView and when you initially start scrolling the UIWebView, have the UIWebView increase in height and have it's Y position decrease at the same time while simultaneously shifting the nav bar up in the Y direction. Once the nav bar has been completely shifted out of view, stop increasing the size of the UIWebView and just allow normal scrolling to occur.
This will give the illusion of the nav bar being part of the UIWebView as it scrolls off the screen.
Also, you'll need to do the reverse when you are scrolling in the opposite direction and are reaching the top of the content of the UIWebView.
Can't give you a straight answer, but have a look at iWebKit. Maybe that provides a solution. The demo, at least, contains a "full screen" item.