Change Button Position on UIScrollView - uiscrollview

how can I change the Position for a UIButton on a UIScrollView in ViewDiDLoad I tryed this but it does not work:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
//I5
Brandwein.frame= CGRectMake(45, 98, 100, 100);
Tee.frame= CGRectMake(178, 377, 100, 100);
} else {
//IP4
Brandwein.frame= CGRectMake(45, 75, 100, 100);
Tee.frame= CGRectMake(178, 338, 100, 100);
}
Tee and Brandwein are the Button's
Using Autolayout
Hope for help.

Try adding it to the viewDidAppear or viewWillAppear method instead of ViewDidLoad.

Add it to viewWillLayoutSubviews - this is the correct place for controller-managed layout-related code.

Related

UIScrollview not scrolling what am i doing wrong

I have an issue where i am adding a UIScrolview to my UIViewController, but my UIScrollview doesnt want to scroll. I want it to work the same way that it works on a UITableViewController.
Here i initialize my UIScrollview
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 800)];
scroll.contentSize = CGSizeMake(320, 800);
scroll.backgroundColor = [UIColor redColor];
scroll.showsHorizontalScrollIndicator = YES;
scroll.scrollEnabled = YES;
scroll.userInteractionEnabled=YES;
[self.view addSubview:scroll];
Here i add my content to the scrollview:
UILabel *memberNo = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
memberNo.text = #"Member No";
memberNo.font = [UIFont boldSystemFontOfSize:16];
memberNo.textColor = [UIColor limeColor];
[scroll addSubview:memberNo];
memberText = [[UITextField alloc] initWithFrame:CGRectMake(10, 40, 300, 30)];
memberText.placeholder = #"Member No";
memberText.borderStyle = UITextBorderStyleRoundedRect;
memberText.text = #"1111111";
[scroll addSubview:memberText];
Try this
set content size more than the screen size and it will scroll whether there is more content or not
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 800)];
HorizontalScroll
scroll.contentSize = CGSizeMake(320, 900);
VerticalScroll
scroll.contentSize = CGSizeMake(340, 800);
As others have said, the scroll view's size (specifically, its bounds.size) must be smaller than its contentSize for it to actually scroll.
You probably want your scroll view to fit inside its superview, so try setting its frame to the superview's bounds:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
The content is the same size as the scrollview: there is nothing to scroll.
The scrollview needs to contain a view that is bigger than it's own size.
Try setting the contentSize to be larger than the frame. For example:
scroll.contentSize = CGSizeMake(1000, 800);
If that doesn't work, then check where you are setting the content size. Where are you initializing the UIScrollView? Try moving the above line to viewWillAppear or viewDidLayoutSubviews.
frame of the scroll view refers to the size of the scroll view itself whereas content size is the size for the view which is to be shown in the scroll view. If the size of view to be shown in scroll view is having smaller size than the scroll views frame size, then it won't at all scroll, but if you set content size more then scroll view has to scroll to show the 'extra' size more than its own frame size.
e.g. If height of scroll view is 100 and you have set the height factor in content size equal to 150, then these extra 50 pixels will be scrollable.
Good Luck!

How do I change an image based on the size of the iphone screen? Please assist me [duplicate]

This question already has answers here:
How to develop or migrate apps for iPhone 5 screen resolution?
(30 answers)
Closed 9 years ago.
On the front page of my app I have two images, which are laid over a portion of two buttons. The buttons are automatically re-scaled (via depending on the size of the iPhone screen. Unfortunately when the images are re-scaled it ends up being pretty ugly.
Can I dictate the size and location of the two images (via code), depending on the size of the iphone screen? If so how would I go about doing so?
Thanks for helping a novice out!
Update 13MAR13
This is the code I tried before. It causes no errors, but no images appear on the page!
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
UIImageView *myImageView = [[UIImageView alloc]initWithFrame: CGRectMake(220, 2.5, 95, 75)];
myImageView.image = [UIImage imageNamed:#"BFS.png"];
UIImageView *myImageViewtwo = [[UIImageView alloc]initWithFrame: CGRectMake(300, 100, 95, 75)];
myImageViewtwo.image = [UIImage imageNamed:#"RMS.png"];
}
if(result.height == 568)
{
UIImageView *myImageView = [[UIImageView alloc]initWithFrame: CGRectMake(100, 400, 95, 75)];
myImageView.image = [UIImage imageNamed:#"BFS.png"];
UIImageView *myImageViewtwo = [[UIImageView alloc]initWithFrame: CGRectMake(100, 300, 95, 75)];
myImageViewtwo.image = [UIImage imageNamed:#"RMS.png"];
}
}
You can take 2 different imags & UIImageViews behind transparent buttons and set their sizes according to screen size.
Code for get screen size:
int h = [[UIScreen mainScreen] bounds].size.height;
Then, check
if([[UIScreen mainScreen] bounds].size.height == 480) {
imgVw1.frame = CGRectMake(x, y, w, h);
imgVw1.image = [UIImage imagenamed:#"....png"];
} //iPhone-4
if([[UIScreen mainScreen] bounds].size.height == 568) {
imgVw1.frame = CGRectMake(x, y, w, h);
imgVw1.image = [UIImage imagenamed:#"...#2x.png"];
} //iPhone-5
You can take two images for it.
imgButton.png -> Normal
imgButton#2x.png -> Retina Display
Edited Code
In .h file,
#property(nonatomic, retain) IBOutlet UIButton *btn;
In. m file,
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
[btn setFrame:CGRectMake(75, 20, 175, 100)];
[btn setImage:[UIImage imageNamed:#"Img_Circle.png"] forState:UIControlStateNormal];
}
if(result.height == 568)
{
[btn setFrame:CGRectMake(65, 60, 200, 120)];
[btn setImage:[UIImage imageNamed:#"Img_Circle#2x.png"] forState:UIControlStateNormal];
}
}
As we already declared property in .h file, then no need to alloc again here.Just simply set frames for it.
Hopefully, this will help to you.
Thanks.

IOS: Popover position

I have this code:
if([self.popoverController isPopoverVisible])
{
[self.popoverController dismissPopoverAnimated:YES];
return;
}
UIViewController* popoverContent = [[UIViewController alloc]init];
popoverContent.view = tableView1.view;
self.popoverController = [[UIPopoverController alloc]
initWithContentViewController:popoverContent];
[self.popoverController presentPopoverFromRect:CGRectMake(1200, 280, 50, 50) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:TRUE];
[[[popoverController contentViewController] view] setAlpha:1]; //alpha del contenuto del popover
[popoverContent release];
It work fine, but I'm not able to place the popover in the lower right, I try to change values in
[self.popoverController presentPopoverFromRect:CGRectMake(1200, 550, 100, 100) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:TRUE];
but I want this popover small in lower right...how can I do?
I solved in this way:
[popoverController setPopoverContentSize:CGSizeMake(320.0f, 262.0f)];
[self.popoverController presentPopoverFromBarButtonItem:barButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

DrawRect is not displaying the image

In my Application am trying to display an image in a rectangle and am using DrawRect method.The problem is at the output am seeing nothing.Only blank screen in simulator.Here is the code:
- (void)viewDidLoad {
[super viewDidLoad];
[self drawRect:CGRectMake(78, 43, 222, 138)];
}
- (void)drawRect:(CGRect)rect {
UIImage *myImage = [UIImage imageNamed:#"btbp.jpg"];
CGRect imageRect = CGRectMake(10, 10, 300, 400);
[myImage drawInRect:imageRect];
//[self setNeedsDisplay];
[myImage release];
}
there is no effect in invoking drawRect. instead you should invoke setNeedsDisplay to enforce redrawing.

show animation when addsubview

I want to add a subview with animation. I am using add sub view so it is not showing any animation so I want to show any animation when I am doing this... I am using below code :-
UIViewController *vControllerHome = [[viewTemp alloc] initWithNibName:#"viewTemp" bundle:nil];
vControllerHome.view.frame =CGRectMake(0, 0, 320, 414);
[self.view addSubview:vControllerHome.view];
self.selectedViewController = vControllerHome;
Can any one suggest how I do this?
Here's the code.. Just try it.
PS: Replace myView with the name of the view you want to replace.
CATransition *applicationLoadViewIn =[CATransition animation];
[applicationLoadViewIn setDuration:duration];
[applicationLoadViewIn setType:kCATransitionReveal];
[applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[[myView layer]addAnimation:applicationLoadViewIn forKey:kCATransitionReveal];
here is for animation blocks
[UIView transitionWithView:containerView
duration:0.5
options:UIViewAnimationTransitionFlipFromRight //any animation
animations:^ { [containerView addSubview:subview]; }
completion:nil];
Maybe you can subclass the UIView and override method willMove(toSuperview newSuperview: UIView?)
Here is example:
override public func willMove(toSuperview newSuperview: UIView?) {
super.willMove(toSuperview: newSuperview)
if let _ = newSuperview {
// This view will be added to some view
UIView.animate(withDuration: 0.2, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 30.0, options: .curveEaseInOut, animations: {
//...
}, completion: { (finish) in
})
} else {
// This view will be removed
}
}

Resources