AutoresizingMask Not Working in Click Events - iphone

I have created a custom button in viewDidLoad() Method and set Autoresizingmask. It works well. But When I Put the same code in Button Click event It Doesn't Autoresized. Is there anyother way to access the AutoresizingMask within Click Events? Thanks in Advance.
Enter code here
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.backgroundColor=[UIColor redColor];
button.frame=CGRectMake(20, 373, 73, 43);
button.autoresizingMask=(UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth);
[self.view addSubview:button];
}
-(IBAction)btn_clicked
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:#"Dynamic Btn" forState:UIControlStateNormal];
enter code here
btn.backgroundColor=[UIColor yellowColor];
btn.frame=CGRectMake(20, 150, 73, 43);
btn.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
[self.view addSubview:btn];
}

(void)layoutSubviews
The default implementation of this method does nothing on iOS 5.1 and earlier. Otherwise, the default implementation uses any constraints you have set to determine the size and position of any subviews.
Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.
You should not call this method directly. If you want to force a layout update, call the setNeedsLayout method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded method.
With reference to Apple document in case of AutoresizingMask not working we need override the above method. AutoresizingMask works when the view frame is changed within - (void)layoutSubviews.
enter code here
-(void)layoutSubviews
{
NSLog(#"layoutSubviews called");
CGRect viewsize=[[UIScreen mainScreen]bounds];
view.frame=CGRectMake(0, 0, 320, viewsize.size.height);
}
Trying the Above code Your AutoresizingMask works well in both iPhone4 and iPhone5.

(void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame=CGRectMake(20, 73, 173, 43);
[button addTarget:self action:#selector(btn_clicked:)
forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button];
}
-(IBAction)btn_clicked:(id)sender
{
UIButton btn = (UIButton)sender;
[btn setTitle:#"Dynamic" forState:UIControlStateNormal];
[UIView animateWithDuration: 0.4
animations: ^{
btn.frame=CGRectMake(20, 73, 100, 43);
[UIView commitAnimations];
}completion: ^(BOOL finished){
}];
}

i think you need to typecast your UIButton through its click event. make your touchUpInside event like this
-(IBAction)btn_clicked:(id)sender
{
UIButton *btn = (UIButton*)sender;
[btn setTitle:#"Dynamic Btn" forState:UIControlStateNormal];
//enter code here
btn.backgroundColor=[UIColor yellowColor];
btn.frame=CGRectMake(20, 150, 73, 43);
btn.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
[self.view addSubview:btn];
}
And call this click like this #selector(btn_clicked:) or connect it via Interface Builder
See if this work....

Please use below code may help you:
-(IBAction)btn_clicked
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:#"Dynamic Btn" forState:UIControlStateNormal];
btn.backgroundColor=[UIColor yellowColor];
btn.frame=CGRectMake(20, 150, 73, 43);
btn.autoresizesSubviews = YES;
self.view.autoresizesSubviews = YES;
self.view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
btn.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
[self.view addSubview:btn];
}

Related

How to add a button in UIScrollView with addSubview?

I am new to iOS development and I have a problem with UIScrollView. First of all, I've created a Scroll View in a storyboard and added
#property (retain, nonatomic) IBOutlet UIScrollView *mainScroll;
in view controller handler
In the viewDidLoad method I have the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.mainScroll.contentSize = CGSizeMake(100.0,100.0); // Have to set a size here related to your content.
// You should set these.
self.mainScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.mainScroll.minimumZoomScale = .5; // You will have to set some values for these
self.mainScroll.maximumZoomScale = 2.0;
self.mainScroll.zoomScale = 1;
UIButton* b = [[UIButton alloc] init];
[b setBounds:CGRectMake(.0f,0.f,100.0,100.0)];
[self.mainScroll addSubview:b];
}
But the button does not appear in the simulator. However if I add a button on storyboard in the IB, the button appears and I can scroll the view.
How can I add the button in code?
Try this code
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.mainScroll addSubview:button];
bounds and frame are not the same thing.
Use this code instead...
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Hello" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 100, 44);
[self.mainScroll addSubview:button];
You need to set scroll view contents size, something like the following code.
[self.mainScroll setContentSize:CGSizeMake(width, height)];
Try this :
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Hello" forState:UIControlStateNormal];
button.frame = CGRectMake(self.mainScroll.frame.origin.x + 25 ,self.mainScroll.frame.origin.y +25, 50, 44);
[self.mainScroll addSubview:button];
Best and easiest way to make a button from code is:
UIButton* yourButton = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
[yourButton setTitle:#"Your Title" forState:UIControlStateNormal];
// if you set the button's image the title won't be visible, because button's title is "under" the image. I recomend you to use this if you have a cut and dried image.
[yourButton setImage:[UIImage imageNamed:#"yourImage.png"] forState:UIControlStateNormal];
// if you set the button's background image, button's title will be visible
[yourButton setBackgroundImage:[UIImage imageNamed:#"yourBackgroundImage.png"] forState:UIControlStateNormal];
// you can add target to the button, for handle the event when you touch it.
[yourButton addTarget:self action:#selector(handleToucUpInside:) forControlEvents:UIControlEventTouchUpInside];
// then add the button
[self.view addSubview:yourButton];
- (void) handleToucUpInside:(id)sender{
// here you can do anything you want, to handle the action
}

Programmatically add a UIButton to a Storyboard View

I have a Storyboard View Controller (with my own custom sub class) of which I have a initWithCoder custom method, where I want to popular the view with my own custom objects (I want to maintain the flexibility of doing this programmatically.
Code works fine until [self.view addSubview:button]; is called, then it crashes with: "Could not load NIB in bundle".
All I wanted to do was add a UIButton on my view...
- (id)initWithCoder:(NSCoder*)aDecoder
{
if(self = [super initWithCoder:aDecoder])
{
[self initLoginForm];
}
return self;
}
-(void)initLoginForm
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 170, 100, 30);
[button setTitle:#"Login..." forState:UIControlStateNormal];
[button addTarget:self action:#selector(handleLoginAttempt) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
how about putting the button in the viewDidLoad method, then add it in the initLoginForm.
Something like this:
-(void)viewDidLoad {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 170, 100, 30);
[button setTitle:#"Login..." forState:UIControlStateNormal];
[button addTarget:self action:#selector(handleLoginAttempt) forControlEvents:UIControlEventTouchUpInside];
}
-(void)initLoginForm {
[self.view addSubview:button];
}
or add it in the viewDidLoad then hide it, then in the initLoginForm show it.

iPhone-MPMediaPlayer disable seek forward button

I would like to remove or disable the seek forward button from the MPMediaPlayer.
I have tried to enumerate all the views but apparently I could not find this button.
Does anyone have any idea?
Thanks.
You could always provide a custom control panel, you can hide the default controls with
playerController.controlStyle = MPMovieControlStyleNone;
Then add a subview containing your interface and just link buttons to play/pause start/stop rewinding, and there's an open source implementation of the variable speed slider (OBSlider). You will also need to register for some MP notifications:
MPMovieDurationAvailableNotification
MPMoviePlayerLoadStateDidChangeNotification
MPMoviePlayerPlaybackDidFinishNotification
MPMoviePlayerPlaybackStateDidChangeNotification
The only way I found is to add a transparent button over the seek forward button.
Here is the code:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIButton *button = [[UIButton alloc] init];
if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
[button setFrame:CGRectMake(535, 599, 90, 60)];
else
[button setFrame:CGRectMake(407, 855, 90, 60)];
[button setBackgroundColor:[UIColor clearColor]];
[button setAlpha:0.7];
[button setTag:1200];
[self.moviePlayer.view addSubview:button];
[button release];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
{
UIButton *button = (UIButton *)[self.moviePlayer.view viewWithTag:1200];
[button setFrame:CGRectMake(535, 599, 90, 60)];
}
else
{
UIButton *button = (UIButton *)[self.moviePlayer.view viewWithTag:1200];
[button setFrame:CGRectMake(407, 855, 90, 60)];
}
}
This is for IPAD ONLY! If you would like to do the same on iPhone, please change the position of the transparent button.

iOS - Dynamic Buttons

I'm trying to use dynamic buttons created via code (no IB) in my project and they appear where and how I want them but they don't fire their actions.
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(475, 302, 49, 58);
[button1 setTitle:#"1"
forState:(UIControlState)UIControlStateNormal];
[button1 addTarget:self
action:#selector(goToFirstTrailer)
forControlEvents:(UIControlEvents)UIControlEventTouchDown];
[myImageView addSubview:button1];
-(void)goToFirstTrailer {
[self startAnimator:#"OutsideToTrailer1_" forNumFrames:60];
}
The imageView this is placed on has buttons and User Interaction Enabled On.
Any light you can shed would be much appreciated.
I think you have the wrong signature of the action method change that line to
-(void) goToFirstTrailer:(id)sender {
and where you set the action to
[button1 addTarget:self action:#selector(goToFirstTrailer:) forControlEvents:....
Important is the colon after the message name because I changed the action method signature to include the sender.
Edit I wrote a small sample project with just an imageView in the MainWindow.xib and created a button programmatically as follows
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(0.f, 0.f, 50.f, 50.f);
[button1 setTitle:#"1"
forState:(UIControlState)UIControlStateNormal];
[button1 addTarget:self
action:#selector(goToFirstTrailer:)
forControlEvents:(UIControlEvents)UIControlEventTouchDown];
imageView.userInteractionEnabled = YES; // <--- this has to be set to YES
[imageView addSubview:button1];
[self.window makeKeyAndVisible];
return YES;
}
It is quick and dirty and yes, I am misusing the application delegate as the view controller. I know it is bad.
Here is the action method
- (void) goToFirstTrailer:(id)sender {
imageView.backgroundColor = [UIColor redColor];
}
Setting the userInteractionEnabled property on the parent imageView makes the difference. With it set to NO which is the default, no events are routed to the button.
If myImageView is a UIImageView object, adding a subview to it (like UIButton) only displays the image that object is represented by. If you add that UIButton as a subview of a UIView, it should work perfectly. Try adding an auxiliary UIView to your UIImageView and then add your button to a subview of the new auxiliary UIView, that should solve your problem.
This works fine for me. Note that I changed the event to UIControlEventTouchUpInside to allow the button press down state to be visible first.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(20,400,88,35); //The position and size of the button (x,y,width,height)
[btn setTitle:#"Quit" forState:UIControlStateNormal];
[btn addTarget:self
action:#selector(showAbout)
forControlEvents:(UIControlEvents)UIControlEventTouchUpInside];
[self.view addSubview:btn];

How to add a UIButton at runtime

I am trying to add a UIButton at runtime however it is not visible. What am I doing wrong?
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
btn.frame = CGRectMake(0, 0, 100, 25);
btn.backgroundColor = [UIColor clearColor];
[btn setTitle:#"Play" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(buttonClick:)
forControlEvents:UIControlEventTouchUpInside];
btn.center = self.center;
[self addSubview:btn];
}
return self;
}
First, make sure the initWithFrame: method is being called. If your view is in a Nib, initWithCoder: is being called instead.
Second, is the button the only subview (from your code it looks like it is, but you never know). The button could be hidden behind another subview. Call bringSubviewToFront: if you need to.
Finally, is the view itself visible? Is it big enough to show the button? Given your example, if the view is less than 100 pixels wide, the button won't show because it will get clipped by the view's bounds.
You must release btn and remove ":" in buttonClick:
UIButton *btn= [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
btn.frame = CGRectMake(0, 0, 100, 25);
btn.backgroundColor = [UIColor clearColor];
[btn setTitle:#"Play" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release];
if it still doesn't work, try removing the : at the end of the selector name: #selector(buttonClick)
You don't need to retain the UIButton because it is retained by [self.view addSubview:btn];
First check whether your code is executing the initwithFrame method.
Because if you are loading the view from nib i.e using
NSArray *xibviews = [[NSBundle mainBundle] loadNibNamed: #"MySubview" owner: mySubview options: NULL];
MySubview *msView = [xibviews objectAtIndex: 0];
[self.view addSubview:msView];
Then the initWithFrame part will not be executing.So please check once.