How to draw a overlay view on top of UINavigation and UITabBar - ios

I want to show a over lay view on top of the existing UINavigationBar and UITabBar items int he bottom.
I tried adding the overlay view's view on application window directly but wouldn't work any idea.
Here is my custom view with all constraints
-(void)showView{
animationOverlay=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 400, 400)];
animationOverlay.translatesAutoresizingMaskIntoConstraints=false;
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 100, 100)];
[btn setTitle:#"Go" forState:UIControlStateNormal];
[btn setTranslatesAutoresizingMaskIntoConstraints:false];
[animationOverlay setBackgroundColor:[UIColor redColor]];
NSLayoutConstraint *leftConstraint = [NSLayoutConstraint
constraintWithItem:animationOverlay attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual toItem:self.view attribute:
NSLayoutAttributeLeft multiplier:1.0 constant:0];
NSLayoutConstraint *rightConstraint = [NSLayoutConstraint
constraintWithItem:animationOverlay attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual toItem:self.view attribute:
NSLayoutAttributeWidth multiplier:1.0 constant:0];
NSLayoutConstraint *topConstraint = [NSLayoutConstraint
constraintWithItem:animationOverlay attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual toItem:self.view attribute:
NSLayoutAttributeTop multiplier:1.0 constant:0];
NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint
constraintWithItem:animationOverlay attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual toItem:self.view attribute:
NSLayoutAttributeBottom multiplier:1.0 constant:0];
NSLayoutConstraint *centreXConstraint= [NSLayoutConstraint constraintWithItem:btn
attribute:NSLayoutAttributeCenterX
relatedBy:0
toItem:animationOverlay
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0];
NSLayoutConstraint *centreYConstraint= [NSLayoutConstraint constraintWithItem:btn
attribute:NSLayoutAttributeCenterY
relatedBy:0
toItem:animationOverlay
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0];
[self.view addSubview:animationOverlay];
[self.view addConstraint:leftConstraint];
[self.view addConstraint:rightConstraint];
[self.view addConstraint:topConstraint];
[self.view addConstraint:bottomConstraint];
[animationOverlay addSubview:btn];
[self.view addConstraint:centreXConstraint];
[self.view addConstraint:centreYConstraint];
}
-(void)hideView{
[animationOverlay removeFromSuperview];
}
right now I can do this inside a ViewController's view successfully
Any ideas on how to do this..
I couldn't think a way how to do this without breaking the constraints
any ideas,suggestions are welcome

Did you try to show the VC modally, I think that will cover the tabbar and navigationbar:
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion;
So put your overlay view inside another VC, and then present that VC from your current VC using presentViewController:

Related

Color of view which is a subView of scrollView is not appearing when I am usign auto layout?

I am adding scrollView using constraints and it is working right. But it is showing strange behaviour.
When I am adding frame of scrollView as [SBV setFrame:CGRectMake(0, 0, scrllView.frame.size.width,1000)]; then its color is clearColor always no matter what I am givig to it.
Code:
UIScrollView *scrllView=[UIScrollView new];
[scrllView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:scrllView];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
[scrllView setContentSize:CGSizeMake(self.view.frame.size.width, 1000)];
UIView *SBV=[UIView new];
[SBV setTranslatesAutoresizingMaskIntoConstraints:YES];
[SBV setFrame:CGRectMake(0, 0, scrllView.frame.size.width,1000)];
[SBV setBackgroundColor:[UIColor grayColor]];
[scrllView addSubview:SBV];
When I am adding frame of view as [SBV setFrame:CGRectMake(0, 0,self.view.frame.size.width,1000)]; then its color is same as given my me. Gray in this case
Don't use CGRectMake function instead leave the size as it is. It wont show any strange behaviour.
Setting content size when using Autolayout does have no effect.
Please refer to this answer - UIScrollView contentSize not working
Also this - UIScrollView doesn't use autolayout constraints

How to add container view on scrollview using constraint with item formate

I am new for auto-layouts and i am trying to add one container view on ScrollView using auto-layouts "constraint with item" formate but I can't adding using my code.
I know how to add using visual formate but I want to do this process using constraint with item formate. Please help me!
My code:
#import "ViewController3.h"
#interface ViewController3 ()
{
UIScrollView * scrollView;
UIView * containerView;
}
#end
#implementation ViewController3
- (void)viewDidLoad {
[super viewDidLoad];
scrollView = [[UIScrollView alloc] init];
scrollView.backgroundColor = [UIColor lightGrayColor];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:scrollView];
containerView = [[UIView alloc] init];
containerView.backgroundColor = [UIColor orangeColor];
containerView.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addSubview:containerView];
//Applying autolayouts for scrollview
NSLayoutConstraint * constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
//Applying autolayouts for containerview
NSLayoutConstraint * constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: scrollView attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
[scrollView addConstraint:constraint2];
}
With Autolayout, you don't need to set scrollview's contentSize explicitly, but you have to provide enough clues to let Autolayout system infer what exactly your scrollview's contentSize is.
There are generally two ways to layout UIScrollView with Autolayout, mixed or pure Autolayout.Check this:https://developer.apple.com/library/ios/technotes/tn2154/_index.html.
If it is pure Autolayout, you can achieve that by your containerView's subviews, that is, your subviews do not rely on the scroll view to get their size. Or you can just tie your containerView's width and height to any view outside your scrollview, like self.view, or to a fixed value.
E.g,
You can add four more lines :
constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
[self.view addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
[self.view addConstraint:constraint2];

Use of scroll bar with auto layout

I am using autolayout. As I can add constraints to view only, but in maximum case I have to use scroll bar. So if I am adding view on scrollView with CGRectmake method. Then it is not changing the dimension in landscape mode.
Snapshot in portrait mode:
Snapshot in landscape mode:
Code:
UIScrollView *scrllView=[UIScrollView new];
[scrllView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:scrllView];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
[scrllView setContentSize:CGSizeMake(self.view.frame.size.width, 1000)];
UIView *SBV=[UIView new];
[SBV setFrame:CGRectMake(0, 0, self.view.frame.size.width,1000)];
[SBV setTranslatesAutoresizingMaskIntoConstraints:YES];
[SBV setBackgroundColor:[UIColor redColor]];
[scrllView addSubview:SBV];
UIButton *BTN=[UIButton new];
[BTN setTranslatesAutoresizingMaskIntoConstraints:NO];
[BTN setTitle:#"UPSide" forState:UIControlStateNormal];
[BTN setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[SBV addSubview:BTN];
[SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:SBV attribute:NSLayoutAttributeLeading multiplier:1.0 constant:100]];
[SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:SBV attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:SBV attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
[SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:100]];
UIButton *BTN1=[UIButton new];
[BTN1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[BTN1 setTitle:#"DOWNSide" forState:UIControlStateNormal];
[BTN1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[SBV addSubview:BTN1];
[SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:SBV attribute:NSLayoutAttributeLeading multiplier:1.0 constant:100]];
[SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:SBV attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:SBV attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
[SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:100]];
How I can create a view that should work in landscape and porttrait mode ?
Disable size classes for use of autolayout.(From Interface Builder Document of view controller's File Inspector)
Take One scrollView in ViewController's View.Take One View Inside it.
Now select scrollView and select second option from Bottom bar [Pin].and select All four constraints (Leading,Top,Bottom,Trailing) and Uncheck constarint Margin Button.Add 4 Constraints.
Now select View and select second Option from Bottom bar [Pin] and select All four constraint (Leading,Top,Bottom,Trailing) and Uncheck constarint Margin Button. Select Height Constraint too(if height>568 than set your bottom Constraint 0). Now Add 5 constraints. And select first Option from Bottom bar [Align].And select second last option Horizonatal center in container and add 1 constraint.

Strange navigation controller behavior

I have a view that is pushed onto navigation stack like this:
FriendsDetailViewController *detail = [[FriendsDetailViewController alloc] init];
detail.user = selectedUser;
[self.navigationController pushViewController:detail animated:YES];
Inside a view controller, I have two elements: my custom controls view, that is a view with two buttons and a label inside, and a table view. I am setting constraints to them as shown:
-(void)setupView {
self.tableView = [[UITableView alloc] init];
self.tableView.dataSource = self;
self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
self.tableView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:self.tableView];
self.controlsView = [[ControlsView alloc] init];
self.controlsView.player = self.player;
self.controlsView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.controlsView];
[self setControlsViewConstraints];
[self setTableViewConstraints];
}
-(void)setTableViewConstraints {
NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.controlsView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
NSLayoutConstraint *leadingConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0];
NSLayoutConstraint *trailingConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0];
NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
[self.view addConstraints:#[topConstraint, leadingConstraint, trailingConstraint, bottomConstraint]];
}
-(void)setControlsViewConstraints {
NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.controlsView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:self.controlsView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0];
NSLayoutConstraint *trailing = [NSLayoutConstraint constraintWithItem:self.controlsView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0];
NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:self.controlsView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:30];
[self.view addConstraints:#[top, leading, trailing, height]];
}
But by the end I get unexpected result.
Firstly, my custom controls view is black, although in code the background color is set to white. Secondly, custom controls view is situated just as I expected, but my table View is messed up. Somehow it does not sit on the bottom of my controls view.
I have other view controller without an embedded navigation controller, and the layout is just fine.
It seems like I don't catch how navigation view is embedded in my view controller. I do the whole project without Interface builder, and this strange behavior is really confusing.
Since iOS7, view controllers set scrollViews insets if there's a navigation bar, to make the content go behind the blurred bar (see https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/occ/instp/UIViewController/automaticallyAdjustsScrollViewInsets)
so to fix your tableView you just need to set self.automaticallyAdjustsScrollViewInsets = NO
For the color of the other view, it's weird, but nothing in the code you posted changes the backgroundColor, are you sure you're setting it somewhere else?

UIImagePickerController, custom UIButton and AutoLayout

I'm trying to put a custom button into UIImagePickerController using auto layout constraints.
- (void)createTimerButtonFor:(UIImagePickerController*)picker
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:#"Timer" forState:UIControlStateNormal];
NSLayoutConstraint* constraint1 = [NSLayoutConstraint constraintWithItem:picker.view attribute:NSLayoutAttributeCenterX relatedBy: NSLayoutRelationEqual toItem:button attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
NSLayoutConstraint* constraint2 = [NSLayoutConstraint constraintWithItem:picker.view attribute:NSLayoutAttributeTop relatedBy: NSLayoutRelationEqual toItem:button attribute:NSLayoutAttributeTop multiplier:1 constant:0];
NSLayoutConstraint *heightConstraint =
[NSLayoutConstraint constraintWithItem:button
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:100.0];
NSLayoutConstraint *widthConstraint =
[NSLayoutConstraint constraintWithItem:button
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:100.0];
[picker.view addSubview:button];
[picker.view addConstraint:constraint1];
[picker.view addConstraint:constraint2];
[button addConstraint:heightConstraint];
[button addConstraint:widthConstraint];
}
Is it even possible to do so? Am I doing something wrong?
Dzior, I think it would be quite hard to add the button to the picker view and let autolayout to position it for you. I guess maybe it is not fully using the auto layout.
I have tried to disable it from rotating but all the tricks for before iOS 8 is not working properly on iOS 8.
It will be easier to create an overlay view and put your button in the overlay view because you have full control of the overlay view class.

Resources