Why can't I access UIImageview.center? - ios

I'm working on an old project of mine with a xib file in it. I have a button that changes the position of an UIImageview to 0,0, but it doesn't work and the label stays at its initial position. When I create new project with the exact same code expect it contains a mainStoryboard.Storyboard it works fine. I suppose it has something to do with the xib file and files owner.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)Action1:(id)sender {
imageview.center = CGPointMake(0, 0);
}

If you're using auto layout, changing the frame may not have any effect.
Instead, you need to change the constraints on the view.
You can find a written tutorial on auto layout on Ray Wenderlich's site here or a video tutorial here.

Related

Button and label are not shown in the iOS app using Xcode

I am trying to write a hello world app on Xcode but I have two errors
The first error message is DTAssetProviderService could not start DTXConnection with Simulator 'iPhone 5'. Check the system log for errors. but I still can run the app but when I run the app on simulatior it does not show the button and label I put in the app. It shows just a screen of the app. Here is my codes in the app for ViewController.h and ViewController.m
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UILabel *hellolabel;
}
- (IBAction)button1:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#implementation ViewController
-(IBAction)button1:(id)sender {
hellolabel.text = #"hello world";
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[ super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
And here is my main.storyboard interface
enter image description here
put your button and label in top - left side in your storyboard interface builder for now and you can see it on simulator.
It is because of different screen sizes, size classes and autolayout.
You should learn autolayout, after you can set constraints to your components and this kind of problem will not be faced.
The issue is probably in your main.storyboard, which you haven't linked below. More than likely, as others have said, you've mucked up your constraints.
On your main.storyboard delete any constraints and move all your buttons to the left most side. Also make sure that your width and height read wAny hAny, as that is the best to use for any screen.

UIScrollView doesn’t seem to work in xcode 6 (iOS 8)

I’m trying to make a scroll view in xcode. I used the following code in xcode 5 (which worked):
view.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UIScrollView *ScrollView;
}
#end
view.m
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[ScrollView setScrollEnabled:YES];
[ScrollView setContentSize:CGSizeMake(320, 1005)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I have seen others with this problem but none of their solutions worked for me. Note that i have size set to freedom and auto layout is turned off.
Try this - UIScrollView with Autolayout
OR
try setting your content size in viewDidAppear
Due to a scrollView feature in auto layout, the contentSize is being redefined. To solve this problem, you could disable auto layout or set the content size in viewDidLayoutSubviews()
-(void)viewWillLayoutSubviews {
[super viewDidLayoutSubviews];
ScrollView.contentSize = CGSizeMake(320,1005);
}
So you would want to add this code and delete the ScrollView.contentSize method in viewDidLoad.
I don't see anything immediately wrong with this code. I would check and makes sure that you've connected the IBOutlet properly in the Interface Builder.

IOS UIButton's size gets bigger when touch inside

simple codes..
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
- (IBAction)dragout:(id)sender;
#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.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)dragout:(id)sender {
NSLog(#"-----");
}
#end
and in Storyboard, there is 1 button which size is fit in text "button". Link 'sent event:touch drag outside" to "dragout" IBAction.
When I touch button inside and drag my finger to outside of button, i need move more than button size to call "dragout" IBAction. Nearly 3x width&height size. Why is this happens? Thx for read.

Can't get a UIScrollView to zoom and scroll

I know this has been posted about a million times, but I cannot for the life of me get this Scrollview to work how it should. I have followed about four different tutorials and have spent the last two days attempting to fix this, so I really do hope someone can help me through this (if there happens to be another post that fixes my problem that I didn't see, I'm really sorry).
I have an Imageview inside of a Scrollview, containing a picture I'd like to be zoomable/scrollable when it's zoomed.
In my WDViewController.h file:
#import <UIKit/UIKit.h>
#interface WDViewController : UIViewController <UIScrollViewDelegate>
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
#property (nonatomic, retain) IBOutlet UIImageView *img_wd;
#end
And in my WDViewController.m file:
#import "WDViewController.h"
#implementation WDViewController
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.img_wd;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.minimumZoomScale=0.5;
self.scrollView.maximumZoomScale=6.0;
self.scrollView.delegate=self;
self.scrollView.contentSize=CGSizeMake(1280, 960);
self.scrollView.scrollEnabled = true;
self.scrollView.userInteractionEnabled = true;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Autolayout is disabled, as I noticed that caused problems to other users on Stack Overflow. I made sure that User Interaction and Multiple Touch were enabled in the Storyboard. But when I launch the app in a simulator, it just sits there. No matter how many times I pinch it doesn't zoom. I thought I did everything, but I must have missed a step that I'm just not seeing for some reason. Can anyone give me a hand?
EDIT: After the comments below, here's the code I have now (for WDViewController.m, .h hasn't changed):
#import "WDViewController.h"
#implementation WDViewController
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.img_wd;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.scrollView addSubview:self.img_wd];
self.img_wd.frame = CGRectMake(0, 0, 1280, 960);
self.scrollView.minimumZoomScale=0.5;
self.scrollView.maximumZoomScale=6.0;
self.scrollView.delegate=self;
self.scrollView.contentSize=CGSizeMake(1280, 960);
self.scrollView.scrollEnabled = true;
self.scrollView.userInteractionEnabled = true;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
EDIT: And after testing it on my phone, it works. Um... okay then, thanks everyone. My simulator just refuses to accept it. How odd.
Just a hunch:
To pinch in the iOS Simulator, you have to hold down the option key.
View hierarchy in IB:
The scroll view was dragged and dropped onto the root view and was snapped into place to fill the entire root view. Image view was dragged and dropped onto the scroll view and was snapped into place to fill the entire scroll view.
Attributes for image view in IB:
All source code:
did you add the image view to scrollview's subview? [self.scrollView addSubView:self.img_wd]

Hello World Objective-C Issue

I have recently started looking into basic IOS app development I am using the following tutorial online https://www.youtube.com/watch?v=GHK3oREwVls&list=PLhAWmh1PlbzGrr8PGLvJBtJ7qniLVTQ9E and I seem to have hit a stumbling block, below are my two view controllers. The error I am getting is regarding the displaytext.text section of the view controller M. The error appearing is;
HelloCocoaViewController.m:30:4: Use of undeclared identifier 'displaytext'; did you mean '_displaytext'?
When I make the suggested change I get a Thread 1: signal SIGABRT around the below code.
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([HelloCocoaAppDelegate class]));
Below is my the code I am currently using.
//
// HelloCocoaViewController.h
// Test
//
#import <UIKit/UIKit.h>
#interface HelloCocoaViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *displaytext;
- (IBAction)hellobutton:(id)sender;
- (IBAction)byebutton:(id)sender;
#end
//
// HelloCocoaViewController.m
// Test
//
//
#import "HelloCocoaViewController.h"
#interface HelloCocoaViewController ()
#end
#implementation HelloCocoaViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)hellobutton:(id)sender {
displaytext.text=#"Hello";
}
- (IBAction)byebutton:(id)sender {
displaytext.text=#"Bye";
}
#end
I will appreciate any and all help.
Edit: I have now tried to implement the solutions suggested (I hope this is correct) however I am sill experiencing the same SIGABRT issue.
#implementation HelloCocoaViewController
#synthesize displaytext;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)hellobutton:(id)sender {
self.displaytext.text=#"Hello";
}
- (IBAction)byebutton:(id)sender {
self.displaytext.text=#"Bye";
}
#end
as you are not using #synthesize to define a backing variable for the property it will implicitly add on as _<propertyname>
so either use the property as self.displaytext or access the property's backing variable directly via _displaytext
Since Apple switched the default compiler from GCC to llvm Objective-C evolves very fast. WWDC videos are a great way to keep track of those improvements
WWDC 2013: Advances in Objective-C
WWDC 2012: Modern Objective-C

Resources