Can't Wire to Subview in IB - ios

Quick question. Using IB, I have a subview in a ViewController. In that subview I have a label, which I would like to wire to my custom subview class. However, IB will not let me. What am I missing?
I also tried to add the label programmatically; however, it appears that the frame was not ever set. I could hard code the size of the label, but I could not make it dependent on the frame size of my subview, because the frame and the bounds were always zero rects, even after the view showed up in my view controller at a non zero size. Any ideas here would also be much appreciated.

You are actually completely right. It wont let you connect from IB to the Header of a custom view in Xcode 4.6.2
Personally I would file a Radar but I would want to do a bit more research to prove it and as this is a pattern I wouldn't ever use then I won't.
Fortunately you can get around it
Make sure your custom view is configured correctly in IB
and assuming you are setup something like this
Then you can manually declare in your header
#interface MyCustomView : UIView
#property (weak) IBOutlet UILabel *label;
#end
And drag FROM the dot that appears beside the property TO the label.
Or drag FROM the right-click HUD of the custom view TO the label.
Neither case will work by dragging from the label to the view.

In your header file, you need to define the label as an IBOutlet, then you can drag from your file's owner to the label.
IBOutlet * lblSomeLabel;

Disable AutoLayOut and try again.

Related

Using auto layout and creating dynamic interface (realigning automatically when field hidden)

Using Xcode5 and auto layout. Consider following scenario:
I have an outlet for "Dispatch" UITextField
#property (weak, nonatomic) IBOutlet UITextField *dispatchTextField;
If user clicks "Camera" button I want to hide dispatchTextField and move "Subject" and "Body"(below) up.
This is not a real scenario but I'm facing tasks where I will be using this kind of technique. I've seen code samples where container size can be modified and so on. In XAML - there is "StackPanel", in Android there is similar controls where I can just hide this TextField and views below automatically spring up.
So, what is the proper way to do this in XCode5 with auto-layout?
I tried (with no luck)
self.recipientTextField.hidden = YES;
I also tried
[self.recipientTextField setFrame:CGRectMake(0, 0, 0, 0)];
[self.view layoutIfNeeded];
Add a height constraint to the text field. After making sure there are no conflicting vertical constraints, add a reference to this constraint in your view controller.
Add an action to the Camera button and connect it to the view controller. Whenever the button is tapped, you would set the constant property of the height constraint to zero.
Make sure that other views are setup to have constant vertical spacing with the text field you wish to shrink.

Modify an existing UIView ios

I've tried to modify a few traits of an existing UIView such as its frame and its backgroundColor, and nothing happens any time I do it. I've looked at some of the answers on here, but none of them have worked for me. I've been able to modify a UIView and then place it as a subview before, but never one that was in the xib or storyboard from the start (xib because I've been working an old project at my work). To be clear the views I've been trying to modify are not self.view, always subviews that were created before hand.
example code:
#property (strong, nonatomic) IBOutlet UIView *toEdit;
[_toEdit setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, 200, 200)];
As explained here, have you tried connecting the elements in the xib to the code throughout Outlets? Ctrl+click on the ui element in the Interface Builder element and release on the relative property of the relative header.
In case you use an Autolayout, the Frame system changes totally and you will not be able anymore (lets say) to edit position directly if not using Constraints.

UIImageView shows in storyboard but doesn't show up in simulator

I added images using uiimageview in storyboard and connected them to iboutlets and they are not showing up in the simulator. I don't think they are off screen since they are very close to other images and I have auto layout off as i saw in other threads that may be a problem. This is some code if it may help. None of the hearts will show up in simulator.
EDIT: I just moved one of the other images to the center of the screen and when I clean/build/run nothing changed in the simulator. So for some reason anything I change in storyboard is not being carried over into simulator. It all worked last night and I'm not sure what could have change to this afternoon.
#import <UIKit/UIKit.h>
float LeftMovement;
float RightMovement;
float UpMovement;
int life = 3;
#interface Game : UIViewController
{
IBOutlet UIButton *Start;
IBOutlet UIImageView *Runner;
IBOutlet UIImageView *Water;
IBOutlet UIImageView *Heart1;
IBOutlet UIImageView *Heart2;
IBOutlet UIImageView *Heart3;
IBOutlet UILabel *Lives;
NSTimer *Movement;
}
-(IBAction)StartGame:(id)sender;
-(void)Moving;
-(void)Walk;
#end
i came up late but the right answer is
Right click on the image in Xcode, go to File Inspector, i had to check the checkbox under "Target Membership"
I bet the use auto layout flag is checked for your storyboard by accident. Open your storyboard file, select the file inspector, and make absolutely sure that the "Use Autolayout" checkbox is NOT checked.
We'll need to see how things look in your .xib file. Is the "image" property set with the proper image name? Are you sure the heart images are added to the bundle? To see if the issue is in fact with the positioning of the image views, give the imageviews a background color and see if they are visible.
Not sure if its too late but anyways.. Did you check in your View container within the attributes inspector that Alpha is set to 1? Because, if it's set to 0 it won't display anything within your view.
I had the same type of problem for me it was a format issue. I exported a .gif which worked fine in the inspector , but for some reason the sim wasn't having it. A simple change to .png did the trick
The issue for me was resolved (especially running in various simulator devices), with the ImageView, selecting in the Attributes Inspector -> View-> Content Mode-> Scale To Fill.
Newbie answer but what happened to me is that I called loadView() even though I wasn't adding anything programmatically. loadView() overrides the storyboard.

make scroll view glitch in xcode?

so i was making a scroll view in my app so i can have more content showing in one view. the storyboard has a lot of text, 1 picture, and 3 buttons at the end. every time i scroll all the way to the button and click on one of them it takes me to the corresponding storyboard but than when i click the button to go back to the main storyboard with all the content, it doesn't led me scroll back to the top. this is all the code in the .m file for the scroll view:
-(void)viewDidAppear:(BOOL)animated{
[myScroll setScrollEnabled:YES];
[myScroll setContentSize:CGSizeMake(320, 940)];
}
this is the code in the .h file for the scroll view:
#interface SimplifyingNumericlaExpressionsViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIScrollView *myScroll;
#end
is there any way i coud set the default coordinates of the scroll view of when it is being view? or something that can fox that?
herei s the link of a video showing the problem http://youtu.be/TgTaiEEhPhc
any idea or example counts. im a beginner :(
thanks!
UPDATE
i tried putting the same code i had in the viewDidLoad and this is what happen:
http://youtu.be/R6M4gGyLxgQ
im now having a different problem, it doesn't scroll all the way down and even though i tried changing the values it still doesn't work. any help? thanks!
the things you are doing should be done in viewDidLoad not viewDidAppear. I would assume the bug is comming from the fact your scroll view was super low already when you set the content size possibly reanchoring your scrollView lower?

UIView subview auto resizing

I wrote a little UIView subclass to show a progress HUD. That HUD view works perfect but I faced a little problem in the last days.
In my application I'm presenting a UIViewController in a custom way. When the user selects a row in a tableView I'm creating an instance of my second viewController, move it to the current VC, set it's view's height to zero, add it as subview of the curren VC's view (at the position of the selected cell) and animate the height back to original.
The behaviour looks pretty cool and works great.
But when the second view is added as subview, I'm adding a HUD to this view. When the second view is resizing to the original height, the HUD sticks to the top of the view and is just a few pixels high:
I played around a bit with NSLayoutConstraints... But I didn't get it working until now...
Has someone a good idea on that one? Or does anybody know good and well explained resources on these constraints?
The HUD is actually a background view with the little window as subview. All other elements (the progress view, labels and so on) are subviews of the little window.
In terms of good resources, I definitely recommend WWDC2012's 3 videos: Introduction to Auto Layouts for OSX/iOS, Auto Layout by Example, and Best Practises for Mastering Auto Layout. These have some tips for looking at ambiguity in the layout and dealing with conflicts
Another great reference is Erica Sadun's iOS6 recipe book.
Re your problem. I'm assuming that you're not seeing an error message and you're laying out the progress HUD entirely in the XIB. If so, it sounds like you have two constraints that aren't behaving as you'd like from the xib - the height from the top of the superView and the height of the HUD
Firstly, create an outlet for the constraints to the .h file
#property (strong) IBOutlet NSLayoutConstraint *HUDSuperViewToHUDConstraint;
#property (strong) IBOutlet NSLayoutConstraint *HUDHeight;
Next, in the method in which you open the new viewController with the HUD remove the constraints so that there is no conflict when you first show the new view
[HUDSuperView removeConstraint:self.HUDSuperViewToHUDViewConstraint];
[HUDSuperView removeConstraint:self.HUDHeight];
After you've called [HUDSuperView layoutIfNeeded] for the first time, in the animation or wherever, add the constraints and call layoutIfNeeded again
[HUDSuperView addConstraint:self.HUDSuperViewToHUDViewConstraint];
[HUDSuperView addConstraint:self.HUDHeight];
[HUDSuperView layoutIfNeeded];
If you call these within an animateWithDuration it may even animate the appearance...cheesey
Hope this helps - it's probably more of a step toward the solution rather than the solution itself. Recommend minutes 17 and 53 in the AutoLayout by Example video too.
Steve

Resources