button outside the scrollview - ios

I have set my scrollView only on a part of the screen.
UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)];
scrollView.contentSize=CGSizeMake(320,1500);//568
[self.view addSubview:scrollView];
Also, out of the scrollView I have set a button.
UIButton *hintbtn = [[UIButton alloc] initWithFrame:CGRectMake(160,self.view.frame.size.height - 50,160,50)];
[hintbtn addTarget:self action:#selector(hint:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:hintbtn];
If you press this button, I want to make the scrollView to scroll down.
-(void)hintbtn:(UIButton*)button {
[scrollView setContentOffset:CGPointMake(0, self.view.frame.size.height - 50) animated:YES];
}
However, it gives an error saying "unknown receiver 'scrollView'". Is there a way to fox this?
Please help me, how to deal with this problem.

That's happening because you don't have a reference to your scrollview variable. To fix the issue please do the following:
In your class' header file (The *.h file) add the following:
#interface YOUR_CLASS_NAME : UIViewController
// Add the following line to the header file
#property (nonatomic, strong) UIScrollView *scrollView;
#end
Then, in the implementation file (The *.m file) add the following:
// Wherever you have this code. It's probably in 'viewDidLoad'
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)];
self.scrollView.contentSize=CGSizeMake(320,1500);//568
[self.view addSubview:self.scrollView];
// In the following line you have a typo. The selector should be "hintbtn:"!
[hintbtn addTarget:self action:#selector(hintbtn:) forControlEvents:UIControlEventTouchUpInside];
// Also modify this code:
-(void)hintbtn:(UIButton*)button {
[self.scrollView setContentOffset:CGPointMake(0, self.view.frame.size.height - 50) animated:YES];
}
Let me know if you have more questions.
Hope this helps!

Related

UIButton as a subview not responding

I have a UIButton inside of a UIView inside of a UIImageView that is not responding to touches. I can't figure out why. I've broken the code down to its simplest form while still doing what my original program does.
#import "TestVideoViewController.h"
#interface TestVideoViewController ()
#property (strong, nonatomic)UIImageView *panel;
#property (strong, nonatomic)UIView *panelSC;
#property (strong, nonatomic)UIButton *panelButtonSC;
#property (strong, nonatomic)UIButton *videoButton;
#end
#implementation TestVideoViewController
-(void) viewDidLoad {
_panelButtonSC = [UIButton buttonWithType:UIButtonTypeCustom];
[_panelButtonSC addTarget:self action:#selector(buttonPressedSC:) forControlEvents:UIControlEventTouchUpInside];
[_panelButtonSC setTitle:#"Open" forState:UIControlStateNormal];
_panelButtonSC.frame = CGRectMake(511, 701, 66, 67);
_panel = [[UIImageView alloc] initWithFrame:CGRectMake(100, 768, 824, 600)];
_panel.backgroundColor = [UIColor whiteColor];
_panelSC = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 784, 560)];
_panelSC.backgroundColor = [UIColor whiteColor];
_videoButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_videoButton addTarget:self action:#selector(playVideo:) forControlEvents:UIControlEventTouchUpInside];
[_videoButton setTitle:#"Play" forState:UIControlStateNormal];
[_videoButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
_videoButton.frame = CGRectMake(400, 400, 200, 40);
[_panelSC addSubview:_videoButton];
[_panel addSubview:_panelSC];
[self.view addSubview:_panel];
[self.view addSubview:_panelButtonSC];
}
- (IBAction)buttonPressedSC:(UIButton*)sender {
[self animatePanelUp];
}
- (IBAction)playVideo:(UIButton*)sender {
NSLog(#"play video");
}
- (void) animatePanelUp {
[UIView animateWithDuration: 0.5
delay: 0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
_panel.frame = CGRectMake(47, 166, 929, 602);
}
completion:nil];
}
#end
I've looked at every similar answer here. To the best of my knowledge all of my framing is correct. There has got to be something simple and stupid that I'm missing. Why doesn't the "playVideo" method ever fire? The only time it works is if I add the button directly to self.view.
UIImageView keeps userInteractionEnabled as false by default - add following line of code it will work.
_panel.userInteractionEnabled = true;
As you are adding a few subview upon one another, check that your button is clickable and no other subview is eating up the touch event, and overall make sure that your views are user action enabled.
Hint:
There might have some problem with the imageview used, as it will have user interaction disabled by default.
Use UIView instead or do this:
_panelSC.userInteractionEnabled = true;
Make sure user interaction is enabled of the parent view. UIImageView by default has userInteractionEnabled false.
_panel.userInteractionEnabled = true

UIScrollView going in infinite recursion

I want to implement a UIScrollView in my controller because the height of the view is greater than the iphone screen size.
I have my view designed in the story board for which I have created an IBOutlet in .h file
In .m, I am doing the following for creating UIScrollView:
-(void) addScrollView
{
UIScrollView* ScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.ScrollView addSubview:self.myStoryBoardView];
self.ScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:self.ScrollView];
}
In the last line of the above, it goes in infinite loop.
Kindly suggest
Replace this line:
UIScrollView* ScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
With:
self.ScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
Explanation:
You are initializing a separate local UIScrollView in your addScrollView method. You local object declaration overrides class instance's object declaration. However following code lines:
[self.ScrollView addSubview:self.myStoryBoardView];
self.ScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:self.ScrollView];
are called on your class instance UIScrollView's object, which is uninitialized. That's why when you add uninitialized object as subview on any object, your app gets crashed.
You seem to make an UIScrollview (called ScrollView) yet you add self.ScrollView, which is probably a defined IBOutlet property (in your .h or .m file in the interface) made in Storyboard. Try this code and get rid of the UIScrollView in your Storyboard.
Also don't use ScrollView but instead use scrollView as apple documentation suggests.
-(void) addScrollView
{
UIScrollView* scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView addSubview:self.myStoryBoardView];
scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:scrollView];
}
He is right. Either add scroll view in the storyboard or from the code. You are setting height of scroll view equal to the height of your self.view instead set height to self.myStoryBoardView.
-(void) addScrollView
{
UIScrollView* scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView addSubview:self.myStoryBoardView];
scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.myStoryBoardView.frame.size.height);
[self.view addSubview:scrollView];
}

How to programmatically display a view -- with an "x" button to close the view?

I am trying to programmatically display a view containing an image and an "x" button -- in the code it is labeled *btnDismiss -- to close the view.
Here is my issue:
The code will successfully display the image and the "x" button. However, clicking the "x" button currently doesn't close the view -- Both the image and the "x" button remain visible.
I'm new to xCode, so please provide verbose responses (if you leave out something from your answer, I might not be able to fill in the blanks!)
Note: If you have another solution for how to show an imageView with an "x" button to make both the imageView and the "x" button disappear, that will also be welcome.
Here is my code:
#import "XYZViewController.h"
#interface XYZViewController ()
#end
#implementation XYZViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIViewController *vc = [[UIViewController alloc] init];
[self presentViewController:vc animated:YES completion:nil];
UIImageView *bgImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 200, 200)];
bgImage.image = [UIImage imageNamed:#"image"];
[self.view addSubview:bgImage];
UIButton *btnDismiss = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
[btnDismiss setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnDismiss setTitle:#"x" forState:UIControlStateNormal];
[btnDismiss addTarget:self action:#selector(dismissVC:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnDismiss];
}
-(void)dismissVC:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
Thanks in advance!
Your code is set up to dismiss a view controller that was presented modally. But the views you want to remove are subviews of self.view.
You can remove the UIImageView and UIButton by keeping outlets to them and calling removeFromSuperview in your remove method. With this method, you don't need to present or dismiss another view controller.
-(void)removeViews:(id)sender {
[self.xButton removeFromSuperview];
[self.imageView removeFromSuperview];
}
Edit in response to comment
You'll need to have properties declared for xButton and imageView
#property UIButton *xButton;
#property UIImageView *imageView;
Edit to show full code
#interface MJNViewController ()
#property UIButton *xButton;
#property UIImageView *imageView;
#end
#implementation MJNViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 200, 200)];
self.imageView.image = [UIImage imageNamed:#"myImage"];
[self.view addSubview:self.imageView];
self.xButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
[self.xButton setTitle:#"x" forState:UIControlStateNormal];
[self.xButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.xButton addTarget:self action:#selector(removeViews:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.xButton];
}
-(void)removeViews:(id)sender
{
[self.imageView removeFromSuperview];
[self.xButton removeFromSuperview];
}
#end
You need to have another UIViewController presenting XYZViewController in order for -dismissViewControllerAnimated: to work properly.
From the Apple Docs:
The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.
Since there is no view controller being presented, calling -dismissViewControllerAnimated: is not working for you.

No UIView appearing in the interface

NOTE: Jump to end for answer.
I am new to IOS Programming. How can I add a visible UIView to the following code. So far it just has the nav bar appear, and black space below, no imageView or the square UIView in the code below.
Below is code in my HomeScreenViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
[square setTintColor:[UIColor orangeColor]];
[self.view addSubview:square];
// Do any additional setup after loading the view.
}
Below is code in AppDelegate.
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.myViewController = [[HomeScreenViewController alloc] init];
UINavigationController *navBar = [[UINavigationController alloc] initWithRootViewController:self.myViewController];
navBar.title = #"Navigation";
self.window.rootViewController = navBar;
[self.window makeKeyAndVisible];
return YES;
}
EDIT:
Here is also the interface for appDelegate. This is literally the only code in the proj other than the generated lines upon creating the project in xcode. I also deleted the storyboard file along with its info in the plist file (otherwise the app would crash).
#import <UIKit/UIKit.h>
#import "HomeScreenViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, strong) HomeScreenViewController *myViewController;
#end
EDITAgain:
No Need to Read Comments, just read this:
Yes, viewDidLoad does invoke.
The background changes to red if i set background color.
No I am not using storyboard.
Still unanswered.
http://postimg.org/image/h5ayao2q3/
FINAL EDIT: Finally! problem solved. It was as simple as setting the background colour for the UIView square. D'oh.
- (void)loadView
{
[super loadView];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.view setBackgroundColor:[UIColor redColor]];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[square setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:square];
// Do any additional setup after loading the view.
}
Thank you very much everyone for your help. I guess my UIImage is borked, but I can figure that out myself.
Try changing it:
self.myViewController = [[HomeScreenViewController alloc] init];
TO:
self.myViewController = [[HomeScreenViewController alloc] initWithNibName:#"HomeScreenViewController" bundle:nil];
And Make sure there are some label or some thing in HomeScreenViewController. Other part of your code looks fine. Hope this helps. :)
First put breakpoint and verify that ViewDidLoad method is getting called.
If not , since you mentioned that you are creating the views programmatically you need to override the - (void)loadView method and create the view for the view controller.
-(void)loadView {
self.view = [[UIView alloc] init];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
[square setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:square];
}
Changed the line [square setTintColor:[UIColor orangeColor]]; to [square setBackgroundColor:[UIColor orangeColor]];
right click your UIViewController and check view connection is established to the xib or storyboard view.
ViewDidload will not get called unless view loads from storyboard or xib.
loadView method gets called if you are creating UIView programatically
If you are not using story board or XIB then you need to add the following method
-(void)loadView{
//load the views
}
I have two options to solve this problem:
1) If you have XIB file corresponding to the controller:
HomeScreenViewController *homeScreenViewController = [[HomeScreenViewController alloc] initWithNibName:#"HomeScreenViewController"] bundle:nil];
1a. Verify the name of your XIB is correct.
1b. Goto XIB file of HomeScreenViewController -> File Owner -> Connection Inspector -> Check the view outlet is connected.
2) If you do it programmatically, add this to the end of your loadView function:
self.view = square;
so the final code should look like:
- (void)loadView
{
//[super loadView];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // <-- look at here, your view has changed its frame now
[self.view setBackgroundColor:[UIColor redColor]];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
[square setTintColor:[UIColor orangeColor]];
[self.view addSubview:square];
// Do any additional setup after loading the view.
}

UIScrollView not showing iOS7

I'm try to add a scrollview totally by code to a modal view, but it doesn't show.
I've tried different ways but I can't see the mistake,
Somebody could help me?
here my code:
#interface AddWithScrollOrizontal ()<UIScrollViewDelegate>{
//IBOutlet UIScrollView*scroll;
}
#property (strong, nonatomic) UIScrollView*scroll;
#implementation AddWithScrollOrizontal
#synthesize scroll;
- (void)viewDidLoad
{
self.scroll = [[UIScrollView alloc]init];
self.scroll.delegate = self;
if (IS_IPHONE_5) {
self.scroll.frame = CGRectMake(0, 58, 320, 270);
}else{
self.scroll.frame = CGRectMake(0, 20, 320, 270);
}
self.scroll.backgroundColor = [UIColor redColor];
self.scroll.pagingEnabled = YES;
self.scroll.contentSize =CGSizeMake(1280, 270);
UIView*firstView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 270)];
...
UIView*fourthView = [[UIView alloc]initWithFrame:CGRectMake(960, 0, 320, 270)];
[self.scroll addSubview:firstView];
[self.scroll addSubview:secondView];
[self.scroll addSubview:thirdView];
[self.scroll addSubview:fourthView];
[self.view addSubview:self.scroll];
}
You say you're adding it totally by code, but you're making an IBOutlet to scroll. Did you create it in IB? If not, your problem is that you never instantiate the scroll view. Also, there's no need to create an ivar for scroll, just create the property inside the #interface declaration for your class extension, and refer to it with self.scroll.

Resources