UIView On Top of Other Views Not Working - ios

I do not understand why my code will not place this custom view on top of the main view. Any suggestions would be great.
Here is my code:
.h
#property (strong, nonatomic) IBOutlet UIView *saved;
.m
self.saved = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
self.saved.backgroundColor = [UIColor colorWithRed:0.0/255 green:102.0/255 blue:153.0/255 alpha:1];
[self.view bringSubviewToFront:self.saved];

You need to add as subview [self.view addSubview:self.saved]; before call bringSubviewToFront.
One note: you have declared your property as IBOutlet, this macro is to denote variables that can be referred to in Interface Builder. In case you plan to create and include the subview from the Interface Builder, it is not necessary to initialise and add as subview through code.

Related

Can I set a UIView hidden is true in the viewDidLoad()?

I have designed a UIView in storyboard and the parent view is a tableview. But I want to set it hidden or unhidden depend on my decision.How can I do?
Get the view by tag? Or any other method?
I put up the code:
UIView *myview=[[UIView alloc] viewWithTag:99];
myview.hidden=true;
But it does not work!
Give the IBOutlet to the UIView in storyboard
#property(nonatomic, weak)IBOutlet UIView *subView;
In the ViewDidLoad or ViewWillAppear
put this line
subView.hidden=YES;
You should replace this line UIView *myview=[[UIView alloc] viewWithTag:99];
with
UIView *myview = [self.view viewWithTag:99];
if you have added on the main view and then try setting the view hidden.
Try writing this code in viewDidAppear.

iOS UIKit Dynamics - unable to add bouncy behaviour for a view

I am trying to add a bouncy behaviour (collision detection) to view which contains all the contents like buttons and images named as ViewContent [it should fall from top and collide/bounce with the bottom of screen].
Here is my view Hierarchy:
and Code:
viewController.h
#interface LoginViewController : UIViewController <UIScrollViewDelegate, UICollisionBehaviorDelegate>
#property (weak, nonatomic) IBOutlet UIView *viewContent;
#property (nonatomic, strong) UIDynamicAnimator *animator;
#end
in storyboard I've set the viewContent frame as (0, -568, 320, 568) so that is should come from top
viewController.m
#implementation LoginViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:#[viewContent]];
gravityBehavior.gravityDirection = CGVectorMake(0.0, 1.0);
[self.animator addBehavior:gravityBehavior];
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:#[viewContent]];
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
collisionBehavior.collisionDelegate = self;
[self.animator addBehavior:collisionBehavior];
UIDynamicItemBehavior *contentViewBehavior = [[UIDynamicItemBehavior alloc] initWithItems:#[viewContent]];
contentViewBehavior.elasticity = 0.4;
[self.animator addBehavior:contentViewBehavior];
As a result nothing happens. I'm following this tutorial http://www.teehanlax.com/blog/introduction-to-uikit-dynamics/ and able to add a collision detection on a square with frame (100, 100, 100, 100), but not able to add collision on a view having frame (0, 0, 320, 568).
I'm new and can't figure it out, any ideas/suggestion please.
Update
Nib view added as #Fogmeister suggested.
I've added a separate nib view file named as LoginView and assigned the LoginViewController class so that I am able to add IBOutlets and IBActions from this Nib.
In LoginViewController's ViewDidLoad method just added these lines right under the [super viewDidLoad
self.content = [[[NSBundle mainBundle]
loadNibNamed:#"ViewLogin"
owner:self options:nil]
firstObject];
[self.view addSubview:self.content];
rest of the code is remain same except in initWithItems viewContent is just replaced with this newly created view self.content.
At the moment you can't use AutoLayout and UIKit Dynamics together. They just don't work together at all.
Also, the first rule of AutoLayout is that you can't update the frame of a view directly. So your line self.viewContent.frame = CGRectMake = (0, -568, 320, 568); won't work either.
What you need to do with this view is disable AutoLayout in order to use Dynamics.
Seeing as you are using a storyboard then the problem is that disabling auto layout for one view will disable it for all views.
If you just want the main view to bounce then what you can do is define that view in a separate nib (or in code).
Either way you then need to programmatically add that view to the view controller's view.
Then your dynamics will work.

Programmatically added view objects not showing up in storyboard

I can't see any of the view objects I've added programmatically. I need to link them to IBAction and/or IBOutlet, but without being able to ctrl-click-drag, I can't.
For example, I have this in viewDidLoad:
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(789, 60, 215, 72)];
[imageView setAnimationImages:imgListArray];
[self.view addSubview:imageView];
The animation shows up in the simulator, but not in the storyboard.
How can I either make these objects show up in storyboard (preferred), or link them to IBActions/Outlets programmatically?
As #vikingsegundo said, Image Builder allows you to add objects to views and then connect them to IBOutlets and IBActions in your classes, but if you add things programatically then you are on your own in terms of Image Builder/Storyboards.
You can still create an iVar or property (preferred) to store the reference to your object, but you don't need the IBOutlet tag, so
#property (strong,nonatomic) UIImageView *imageView;
and then
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(789, 60, 215, 72)];
[self.imageView setAnimationImages:imgListArray];
[self.view addSubview:self.imageView];
You can also add action handlers to objects through code (the equivalent of ctrl-dragging to an IBAction method)
[self.aButton addTarget:self action:#selector(buttonEventHandler) forControlEvents: UIControlEventTouchUpInside];
To set a code-instantiated object as an instance variable, just set it. Like this:
// your code
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(789, 60, 215, 72)];
[imageView setAnimationImages:imgListArray];
[self.view addSubview:imageView];
// and then:
self.myImageView = imageView; // assumes you've got a `#property` called `myImageView`
That's exactly the same thing that constructing an outlet connection in the storyboard does, but now you're doing it yourself.
To set a code-instantiated control's action, call addTarget:action:forControlEvents:.
That's exactly the same thing that setting up an action connection in the storyboard does, but now you're doing it yourself.

Pass Subview to subclassed UIView

I have a subclassed UIView with a special behavior.
It is a custom class of a ViewController xib view. It contains a subview, which is currently created programmatically.
My question is; how to pass a subview either from the xib or from one created programmatically in the viewController via initWithFrame or initWithCoder to my subclassed view? (Right now it simply acts through the view, not initiated at all in the VC.)
I would share code, but I don't know that it's necessary.
Use the initWithFrame method:
SubclassedView *view = [[SubclassedView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:view];
Be sure to import #import "SubclassedView.h"

why my actual UI layout is not same with my designed layout in xib

this is what I designed in xib, and it's my expectation also.
My ViewControlle.h file:
#interface DetailHeadViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIScrollView *slideScrollView;
#property (weak, nonatomic) IBOutlet UIImageView *smallImageView;
#property (weak, nonatomic) IBOutlet UIImageView *barBGVImageView;
#end
My ViewController.m file (part of the source code)
- (void)viewDidLoad
{
[super viewDidLoad];
slideScrollView.pagingEnabled = YES;
slideScrollView.showsHorizontalScrollIndicator = NO;
slideScrollView.showsVerticalScrollIndicator = NO;
UIImageView * page1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"headSection_hot1"]];
[slideScrollView addSubview:page1]; //Update 3
}
the result is this,
But actually the correct result is this,
Anyone can help to point out what the problem is?
Thanks in advance
update (remove bar image)
for easier, I remove bar image in xib and also comment out the relatived code.
Please see the following picture.
update 2
IB
To me it looks like your scroll view frame is not set correctly. It should probably be set to 0,0,480,320.
It also looks like the transparency isn't set on the BarImageView. Select the BarImageView in the xib, open up the inspector window, click on the 4th tab, set the background color to Clear Color and reduce the alpha.
You may also want to post how your adding the image to the scroll view as that could create issues as well.
Update 1:
In the viewDidLoad of your DetailHeadViewController try this:
self.scrollView.frame = self.view.bounds;
UIImageView * page1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"headSection_hot1"]];
page1.frame = self.view.bounds;
[self.slideScrollView addSubview:page1];
self.scrollView.contentSize = self.view.bounds.size; // Note that the contentSize must be changed
// from this if you want to scroll.

Resources