Elements position get changed after adding them as subView - objective-c

I put a UITextField, a UITextView and another element to a UIView and set their position in Utility Area in Xcode. What I want to do is group them as a subView and apply some animations to the subView.
But after adding them to the newly created subView change their position, look at this pictures (before and after adding them):
Before
After
The green UITextField is center aligned and the UITextView is disappeared! (I don't care about the UILabel and that horizontal line right now).
It's my code:
CGRect oldPosition = taskTitle.frame;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
[view setBackgroundColor:[UIColor redColor]];
[view addSubview:taskTitle];
[view addSubview:taskNote];
[view addSubview:datePickerButton];
taskTitle.frame=oldPosition;
[self.view addSubview:view];

Disable autolayout from the file inspector, in Utilities bar.
It can be done by unchecking the Use Autolayout option.
Here is a screenshot:

Related

Centre a view in its parent, when in landscape

I've search and found a few questions about this, but none containing an answer that worked for me.
This code centres a subview in the current view, if the device is in portrait, but not if it's landscape. How do I make the centring work in landscape?
UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
redView.backgroundColor = [UIColor redColor];
redView.center = [self.view convertPoint:self.view.center fromView:self.view.superview];
[self.view addSubview:redView];
Rather try using
redView.center = [self.view convertPoint:self.view.center fromView:nil];
Works for me.
Note from the docs:
The view with point in its coordinate system. If view is nil, this
method instead converts from window base coordinates. Otherwise, both
view and the receiver must belong to the same UIWindow object
(should be called in viewDidAppear, not in viewDidLoad)

How put a UIView above the keyboard

I have a idea to hide some keys in keyboard. I use the exact background of the keyboard and use it above the keyboard.
So I create a textView and add a accessoryView
//Create the view
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -10, 768, 300)];
[view setBackgroundColor:[UIColor greenColor]]
UITextField text = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 80, 28)];
[text setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
[text setInputAccessoryView:view];
This really hide the keyboard, but if I click in this view, the button still show. I try to use a UIButton, but the button don't catch the click. And I try becameFirstResponder when UIKeyboardDidShowNotification and no success.
Any Ideas?
You can set userInteractionEnabled to YES for your accessory view. This will make your view swallow all the touches.
If you want a custom view for editing, though, you can do this by setting a specific editing view for your field rather than trying to put something else on top of the keyboard.

UIProgressView+Activity Indicator View on a UITableView during data upload

I have a uitableview.
I saw this "effect" and I would replay it:
When the user taps on a button to upload some data on a server, I would a gray (transparent) view with a progress bar (maybe with also an activity indicator) appears on the table.
The table is disabled and can be viewed through the gray view (that is the gray transparent view covers all the table).
How can I achieve this?
Have I create a view with a progressive view on it and then put it in the same xib of the table, disabling it properly programmatically? Or?
Lay a large black UIView over top of the UITableView with an alpha value of 0.5. Then put a spinner (UIActivityIndicatorView) on top of that.
Something like this:
UIView *view = [[UIView alloc] init];
view.frame = myTableView.frame;
// save this view somewhere
UIActivityIndicatorView *ac = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
CGRect frame = view.frame;
ac.center = CGPointMake(frame.size.width/2, frame.size.height/2);
[view addSubview:ac];
[ac startAnimating];
[ac release];
[myTableView addSubview:view];
[view release];
Then remove it later with [view removeFromSuperview]

NSTextView fails when used as contentView

I am programmatically creating a NSTextView to be the contentView for a window. When I create a WebView using the following code, it displays scrollbars, and the resizing thumb, and resizing the window works flawlessly.
When I comment out the WebView and attempt to use a NSTextView as the contentView, it does not "work" when the window is resized: Resizing the window using the thumb causes the content of the text view to not repaint correctly, it also paints over the title of the window, and the resizing thumb is also not repainted.
-(void)applicationDidFinishLaunching:(NSNotification*)aNotification{
NSTextView* view = [[NSTextView alloc] initWithFrame:[window frame]];
// WebView* view = [[WebView alloc] initWithFrame:[window frame]];
[window setContentView:view];
[window makeFirstResponder:view];
[window makeKeyAndOrderFront:view];
}
Edit: Working code. Creates a NSScrollView to be the windows new contentView, and adds an NSTextView as its document.
-(void)applicationDidFinishLaunching:(NSNotification*)aNotification{
NSScrollView* scrollView = [[NSScrollView alloc] initWithFrame:[window frame]];
NSTextView* view = [[NSTextView alloc] initWithFrame:[scrollView bounds]];
[window setContentView:scrollView];
[scrollView setDocumentView:view];
[scrollView setHasVerticalScroller:YES];
[scrollView setHasHorizontalScroller:YES];
[window makeFirstResponder:view];
[window makeKeyAndOrderFront:view];
}
A web view makes and manages its own scrollers and is a special case rather than the norm. An NSTextView does not. It's just the text view. This is the norm - scrollable views come pre-wrapped in an NSScrollView only in the convenience of Interface Builder. In code, you must create an NSScrollView as well, then wrap the view in it. It's the scroll view that would be your top-level view in that case.

How To Draw A Line Dividing The Cell's Accessory View

I just wanted to know how I could draw a line dividing the cell's accessory view from the rest of the cell.
You could simply add a thin UIView (or UIImageView if you wanted a fancier divider) like so.
UIView *divider = [[UIView alloc] initWithFrame:CGRectMake(290, 3, 1, 38)];
[divider setBackgroundColor:[UIColor blackColor]];
[cell.contentView addSubview:divider];
[divider release];
If you're using one of UITableView's default accessory views, your content view will be squashed to make room for the accessory, so you your x-coordinate may have to change to sit at the very edge of the content view.