I am using XCode 4.6.1 and developing for iOS 6.
I have added a button to the storyboard. I have created an outlet in my implementation file ViewController.m:
//
// ViewController.m
//
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIButton *b1;
#end
I try to change the button b1's properties as follows (in this same file: ViewController.m):
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.b1.alpha = 0.5;
self.b1.frame = CGRectMake(0,0,123,412);
}
When I run the app in the simulator, the alpha of the button is successfully set to 0.5.
However, the position and size of the button doesn't change.
I have tried various ways to make it happen. However nothing seems to work. I would like to know what am I doing wrong. I am pretty new to Objective C. Any help would be greatly appreciated.
You are probably using autolayout:
When using autolayout you cannot manually change your outlets frames. You have two options: you can either disable autolayout (then your code should work), or create outlets to your constraints and then modify the constrains programmatically:
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *widthConstraint;
and then in your code:
self.widthConstraint.constant = 123;
and so on with all the constraints that need to be modified.
Related
I'm having a problem that doesn't seem to have an obvious solution. I've searched around and I've been through all common answers that I could find.
My custom xib views don't show up on the app when I launch. The background is clear, and this xib has 5 image views as you can see below which aren't set to hidden.
The class has about 5 delegates as well which I set from a caller when the delegates have been initialized. The caller of initWithDelegates: is the parent UIViewController that displays this xib.
CustomView.h
#interface CustomView : UIView<SomeProtocol>
// UI Items - THESE AREN'T SHOWING UP
#property (strong, nonatomic) IBOutlet UIImageView *image1;
#property (strong, nonatomic) IBOutlet UIImageView *image2;
#property (strong, nonatomic) IBOutlet UIImageView *image3;
#property (strong, nonatomic) IBOutlet UIImageView *image4;
#property (strong, nonatomic) IBOutlet UILabel *label;
#property (strong, nonatomic) IBOutlet UIStackView *centerStackView;
- (id)initWithDelegates:(UIViewController*)delegate1 withDelegate2:(NSObject<Delegate2>*)delegate2 withDelegate3:(NSObject<Delegate3>*)delegate3 withDelegate4:(UIViewController<Delegate4>*)delegate4
withDelegate5:(NSObject<Delegate5>*)delegate5;
#end
CustomView.m
#implementation CustomView
- (void)awakeFromNib {
[super awakeFromNib];
}
- (id)initWithDelegates:(UIViewController*)delegate1 withDelegate2:(NSObject<Delegate2>*)delegate2 withDelegate3:(NSObject<Delegate3>*)delegate3 withDelegate4:(UIViewController<Delegate4>*)delegate4
withDelegate5:(NSObject<Delegate5>*)delegate5
{
NSArray * arr =[[NSBundle mainBundle] loadNibNamed:#"CustomView" owner:nil options:nil];
self = [arr firstObject];
self.delegate1 = delegate1;
self.delegate2 = delegate2;
self.delegate3 = delegate3;
self.delegate4 = delegate4;
self.delegate5 = delegate5;
[self setLoopImage];
[self layoutIfNeeded];
return self;
}
#end
What else I've verified:
I made sure that the xib file is properly selected under "Custom Class" in the interface builder.
I've walked through the code to make sure there are no crashes or obvious issues.
The xib doesn't appear to have any obvious issues in interface builder such as being set to HIDDEN.
The 5 IBOutlet ImageViews get a memory address and aren't nil.
Any help is appreciated!
UPDATE:
I believe this has something to do with putting the nibs in a nested UIView. In the interface builder, the order is UIViewController -> UIView -> nibs
There doesn't seem to be a method to register nibs from the parent UIViewController as there are in classes like UITableView.
I've also tried:
Setting the background color in the xib to make sure it shows up. It doesn't. However if I set the background color to red in the parent where I added the view in interface builder, it turns red. (See Figure 1), and my xib file is shown in Figure 2
Over-rode initWithFrame to make sure its being called. It is called.
Thinking that the view may be instantiated twice, one via autolayout and one via my custom init method, I tried making the (id)initWithDelegates method not init or register the nib in case that was duplicating the object. I did this by removing 1) return self, 2) the lines that register the nib, and 3) making the method prototype return void - I.E. (void)initWithDelegates - This didn't work either.
I tried these lines within initWithDelegates :
[self addSubview: self.image1];
[self bringSubviewToFront: self.image1];
[self.menuImage setHidden:image1];
-- with no luck.
Figure 1: (Actual result)
Figure 2 (Expected.. with red background)
Check your Autolayout in your custom XIB. Something the contain does not show because of it. For example: only center vertically & center horizontally. Try to do different autolayout constraint in your view in custom class.
I think something is wrong with layout of your CustomView inside ParentView. Could you please:
Check the frame of CustomView when you add it into the ParentView.
Check constraints that has CustomView inside ParentView. If you don't
have them - add it, constraints will iOS the glue how to layout your
CustomView. More information about this you can find here Programmatically Creating Constraints
And also you can try to use "Debug View Hierarchy" in Xcode to
inspect what is going on on your screen, there you can find your
CustomView in the view hierarchy and you will see what is wrong.
nibs cannot be nested within the structure.
Instead of:
UIViewController -> UIView -> nibs..
It should be:
UIViewController -> Container View -> UIView with nibs.
#everybody
Hi guys!
I've not found out the solution to my problem, so I decide to ask the question again.
First attempt is there LINK
I have screen w/ a few labels. Each of them might be from 1 to a dozen lines. So some times all content not to fit on screen so scrolling is necessary.
All seem to be easy and there are a lot of tutorials how to do it. Just set contentSize bigger than frame. But my problem that scrolling works only when I zoom in screen. Setting contentSize doesn't give any effect.
#interface OMSScroolViewController : UIViewController <UIScrollViewDelegate>
#property (strong, nonatomic) IBOutlet UIScrollView *scroll;
#property (strong, nonatomic) IBOutlet UILabel *label1;
#property (strong, nonatomic) IBOutlet UILabel *label2;
#end
and implementation
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
scroll.minimumZoomScale=1.0;
scroll.maximumZoomScale=1.0;
[scroll setContentSize:CGSizeMake(320, 1690)]; // constants for simplicity
scroll.delegate=self;
}
I've also tried to use a extra UIView inside UIScroolView but w/o any success.
Moreover I found a few threads there on stackoverflow w/ the same problem, unfortunately w/o solutions.
Thanks for cooperation.
I have looked around many different threads over the same issue and can't seem to grasp what is going on here. I created a very basic and simple project. What I want to happen is for a bunch of elements to be inside of a scroll view that scrolls properly. These elements may include a UITableView that scrolls independently (or not). Here is how I set up my basic project:
Running this in the simulator will result in a view that doesn't scroll. This is expected as there is more than enough room for all of the elements. I then changed the size of UIScrollView like so:
Running this in the simulator results in a smaller view that will not scroll. I assume I am not doing this the correct way. Here is the code I have most recently tried:
ViewController.h
#interface ViewController : UIViewController <UIScrollViewDelegate>
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
#property (strong, nonatomic) IBOutlet UIButton *buttonOne;
#property (strong, nonatomic) IBOutlet UIButton *buttonTwo;
#property (strong, nonatomic) IBOutlet UIButton *buttonThree;
#property (strong, nonatomic) IBOutlet UIButton *buttonFour;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.buttonOne setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.buttonTwo setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.buttonThree setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.buttonFour setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Any help would be greatly appreciated.
The problem is that when you change the size of the scroll view in interface builder, you are also setting the content's size to the same as the frame size.
You can manually increase the content size of the scroll view in code so that you can scroll it like this:
// scrollView is an outlet to your scroll view
// Add 200 points to the bottom of the scroll view
[self.scrollView setContentInset:UIEdgeInsetsMake(0, 0, 200, 0)];
Though, you probably don't need to do this as long as you draw the sub view at full size, and make it a sub view of some other view, and let it automatically re-size itself.
I have updated my Xcode and suddenly all Buttons, which are connected in the storyboard to the selectors, doesn't work anymore. All programmatically coded Buttons and gesture recognizers work. Partly, they are calling the same IBActions.
What have I done...
Just add a Button and a Label to the View in the Storyboard. In the View Controller .h I added a Outlet to the Label and declare the IBAction.
The files:
.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *myLabel;
-(IBAction)button_pressed:(id)sender;
-(IBAction)swipe_active:(id)sender;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize myLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UISwipeGestureRecognizer *swipe_left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipe_active:)];
swipe_left.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipe_left];
}
-(IBAction)button_pressed:(id)sender{
myLabel.text=#"Button is Running";
}
-(IBAction)swipe_active:(id)sender{
myLabel.text=#"Swipe is Running";
}
#end
And just the Button does not work.
StoryBoard: https://i.stack.imgur.com/1hruM.png
I had this same exact problem after upgrading to xcode 4.5. The IBActions appear as though they are set up correctly in IB but if you look at the .h and .m files, you'll see they're not. If you go to your storyboard and click on your view controller, look under the outlets and you'll see a tab for "Received Actions". Hook up your controls to your actions there and they'll work again.
I have been seeing the exact same issue for many of our apps. In all of my cases it was fixed by reconnecting the action in Interface Builder.
At the moment I have a UIViewController subclass with a UIScrollView and a UIView inside of it. Below is a snippet of code from the class:
.h
#import <UIKit/UIKit.h>
#interface Scroller : UIViewController <UIScrollViewDelegate>
{
UIScrollView *scrollView;
UIView *testView;
}
#property (retain, nonatomic) IBOutlet UIScrollView *scrollView;
#property (retain, nonatomic) IBOutlet UIView *testView;
#end
Inside the IB I link the UIScrollview to my variable, but when I get to the .m and try setting my scrollview contentSize it shows the scrollView as being nil.
.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// Initialize of scrollView
scrollView.contentSize = CGSizeMake(500, 500);
scrollView.delegate = self;
}
Am I missing an import or anything to get this to be properly allocated?
Edit
It may help to know, but if I try to debug the issue and in the lldb check the value of scrollView I get a EXC_BAD_ACCESS error if I try to continue in the code.
try
//.h
#interface Scroller : UIViewController <UIScrollViewDelegate>
{
}
#property (retain, nonatomic) IBOutlet UIScrollView *scrollView;
#property (retain, nonatomic) IBOutlet UIView *testView;
//.m
[self.scrollView setContentSize:CGSizeMake(500, 500)];
just to clarify
scrollview without the self. was looking at the iVar - which I assume you had not alloc-ed/init-ed.
The #property IBOutlet connected to your nib is NOT the same as that iVar. In effect you have two variables of the same name - if you get rid of those iVars the property will still get allocated and initialised for you when the XIB is loaded.
The issue wasn't related to the code, but the debugger itself. I tried doing this in xCode 4.3.1 and was debugging through the application and saw that the proper fields weren't being set both in the xib and in the code. I deleted everything and opened up xCode 4.2 and tried in there and everything worked nicely. After testing in 4.2 I moved to 4.3.1 where it asked me to switch from the GDB to the LLDB. I guess it was just paranoia on my end for not seeing proper results showing up.