I am trying to use an image adjacent to back button arrow with some little space.
i have used the code below ,but this have increased space between arrow and image. can anyone tell me how to reduce this space .
UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(-10, 2.0, 200, 42.0)];
UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithCustomView:button1];
button.imageInsets = UIEdgeInsetsMake(0, -100, 0, -25); //this didn't worked out as expected ...i used this for reducnig space
self.navigationItem.leftBarButtonItem = button;
//THis allows to add image / button to the right of back button
self.navigationItem.leftItemsSupplementBackButton = YES;
Related
In a UITableView like below... is there an easy way to move the checkmark buttons from the left side of the UITableViewCell to the right side?
Yes you can do this using UITableViewCell's accessoryView like below:
cell.accessoryType = .Checkmark
cell.accessoryView = UIView(frame: CGRectMake(0, 0, 20, 20)) //You can change to UIButton with your own custom checkmark button
cell.accessoryView.backgroundColor = UIColor.blueColor() //change to your color
I have the following code to dismiss the keyboard if the user taps the background. It works fine if the scrollview is in the PointZero position, but if the user scrolls the view and then selects the textview, it doesn't call the "dismissKeyboard' method until the 2nd background tap.
On the first tap (for some reason) moves the scrollview offset to align with the scrollview frame to the screen bottom. The second tap will dismiss the keyboard and run the code below. I know it has to do with the scrollview. Any help would be appreciated.
Thanks
- (void)viewDidLoad {
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[_scrollView addGestureRecognizer:tapGesture];
}
-(void)dismissKeyboard {
[self.view endEditing:YES];
}
- (void)keyboardWasShown:(NSNotification *)notification {
scrollViewRect = _scrollView.contentOffset.y;
NSDictionary* info = [notification userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
keyboardSize.height += 10;
CGFloat viewBottom = CGRectGetMaxY(self.scrollView.frame);
if ([_itemNotes isFirstResponder]) {
CGFloat notesBottom = CGRectGetMaxY(_itemNotes.frame);
viewBottom -= notesBottom;
if (viewBottom < keyboardSize.height) {
keyboardSize.height -= viewBottom;
CGPoint scrollPoint = CGPointMake(0.0, keyboardSize.height);
[self.scrollView setContentOffset:scrollPoint animated:YES];
}
else {
[self.scrollView setContentOffset:CGPointZero animated:YES];
}
}
else {
[self.scrollView setContentOffset:CGPointZero animated:YES];
}
}
- (void)keyboardWillBeHidden:(NSNotification *)notification {
CGPoint scrollPoint = CGPointMake(0.0, scrollViewRect);
[self.scrollView setContentOffset:scrollPoint animated:YES];
}
EDIT:
So I figured out a solution but it seems like there must be a better way to handle this. The problem was because I was setting the contentOffset of the scrollView so that the contentSize was beyond the screen boundaries. Thus the first tap was moving the scrollView contentOffset back within the screen boundaries and the second was performing the tap gesture. I will post my solution below hoping that someone has a better answer.
I would recommend setting
_scrollView.layer.borderColor = [UIColor redColor].CGColor;
_scrollView.layer.borderWidth = 1;
This will show you exactly where your scrollview boundaries are, which may not be where you think they are, or may be covered by something else. Also, when I open the keyboard, I generally set the scrollview frame bottom to the top of the keyboard. Otherwise, you may have content below the keyboard you can't get to. Not sure if this is exactly related to your issues.
I am assuming there must be a better solution to this but I was able to solve the issue by extending the contentSize when the keyboard is displayed and then shrinking it back down when the keyboard is hidden.
Set a float (scrollViewHeight) to hold the original content size for the reset.
//add this right before setting the content offset
scrollViewHeight = _scrollView.contentSize.height;
_scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width , scrollViewHeight + keyboardSize.height);
//add this right before reseting the content offset
_scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width , scrollViewHeight);
It really seems like there must be a better way that I'm not aware of. I will have to go back through the documentation to see if there is another way.
I'm adapting an iOS 6 app to iOS 7 and I'm experiencing an strange "error". In a screen there's a rightBarButtonItem with a simple image that is showed in his place. But, if the app shows an alertview, the image moves down (50 px or so) when I tap the OK button of the alertview (the only button in this alert). There's no action linked to this alertview, it's only informational.
Also, if I change the image (setImage) of the button, this image will appear out of place.
Well, I finally found myself a solution:
I had a UIBarButtonItem with UIBarButtonItemStylePlain and an image setted with setImage on the UIBarButtonItem.
To solve the issue, I have created an UIButton with the image (setting its frame with an CGRectMake) , and then I have created the UIBarButtonItem with initWithCustomView and using the UIButton as the CustomView. This way the image is always where it should be.
Edit:
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(0.0, 40.0, 30.0, 30.0);
[aButton setBackgroundImage:[UIImage imageNamed:#"anImage.png"] forState:UIControlStateNormal];
[aButton addTarget:self action:#selector(aFunction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *anUIBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:aButton];
self.navigationItem.rightBarButtonItem = anUIBarButtonItem;
Thanks for this raul, I've translated it into Swift for the Swift users out there:
let a = UIButton(type: .Custom)
a.frame = CGRectMake(0.0, 40.0, 30.0, 30.0)
a.setBackgroundImage(UIImage(named: "Share")!, forState: .Normal)
a.addTarget(self, action: "shareThis:", forControlEvents: .TouchUpInside)
let uiItem = UIBarButtonItem(customView: a)
self.navigationItem.rightBarButtonItem = uiItem
I had a similar issue with my right nav bar button and it went away when I removed the "title" value of the offending bar button item. In my case the title should never have been set because the button uses an image. YMMV.
I'm not sure why it matters but it fixed my issue with the bar button item getting offset on uialertcontroller dismissal.
I got my inspiration from this question: UIAlertController moves leftBarButtonItem down
I want to build a button that every time you press rotates the image by 90 °. I tried to do so, but it does not work
(IBAction)Rotate:(id)sender {
staticPictureOriginalOrientation=UIImageOrientationLeft;
[self.imageView setInputRotation:staticPictureOriginalOrientation atIndex:0];
[staticPicture addTarget:filter];
[staticPicture processImage];
}
what I get is this
Tap Button +90° rotate image and processImage
I'm doing the same thing using this code:
selectedFilter = [[GPUImageTransformFilter alloc] init];
[(GPUImageTransformFilter *)selectedFilter setAffineTransform:CGAffineTransformMakeRotation((M_PI)*0.5)];
self.img = [selectedFilter imageByFilteringImage:self.img];
self.mainImage.image = self.img;
self.originalImage = self.img;
I am new to xcode, but I have been searching around for solution, and I have tried many different methods, but none of it is working. I am trying to make UIButton flip to the reverse side by adding animation to it. This will occur when program loads up, so user can just sit back and watch a whole bunch of buttons flip over and over.
The problem that I am having is that the animation seems to take place before screen even loads because I can see a different view (to show that button is flipped), as soon as the program loads, but I cannot see the animation taking place.
What I have done:
I created a main view controller and inside the controller I have different subviews and within those subviews, I have UIButtons. I tried to add a delay to my animation, but it still does not fix it. I don't know if the problem could occur because I create subviews and buttons and then animate under - (void)viewDidLoad. I also tried to create another subview and put just one button into that subview, which will create 2 subviews and each having just one button and animate by flipping between the subviews, but I still get the same problem. The animation still happened before the screen even loads up, instead of during the run time of the program.
I am attaching part of the code that I did to this one.
CGRect button20subviewFrame = CGRectMake(242, 370, 70, 83);
UIView *button20subview = [[UIView alloc] initWithFrame:button20subviewFrame];
UIButton *button20 = [UIButton buttonWithType:UIButtonTypeCustom];
[button20 addTarget:self action:#selector(switchToDetailView:) forControlEvents:UIControlEventTouchDown];
[button20 setTitle:#"Button20" forState:UIControlStateNormal];
button20.frame = CGRectMake(0 , 0 , 70 , 83);
UIImage *button20Image = [UIImage imageNamed:#"t1.png"];
[button20 setImage:button20Image forState:UIControlStateNormal];
[button20subview addSubview:button20];
[self.view addSubview:button20subview];
UIButton *button21 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button21 addTarget:self action:#selector(switchToDetailView:) forControlEvents:UIControlEventTouchDown];
button21.frame = CGRectMake(0, 0, 70, 83);
UIImage *button21Image = [UIImage imageNamed:#"t2.png"];
[button21 setImage:button21Image forState:UIControlStateNormal];
//animation part
[UIView animateWithDuration:3 delay:2 options:UIViewAnimationTransitionFlipFromLeft animations:^{
[button20 removeFromSuperview];
[button20subview addSubview:button21];
}completion:^(BOOL finished){
}];
Any help is appreciated.
Thank you.
viewDidLoad is the problem. It gets called when the view is done loading, which is before it is put on screen. viewDidAppear should do what you're looking for.
In addition to this, you may want to call [button20 removeFromSuperview] in the animations completion handler to ensure that the front of the button isn't removed while the animation is still taking place.
E.x:
[UIView animateWithDuration:3 delay:2 options:UIViewAnimationTransitionFlipFromLeft animations:^{
[button20subview addSubview:button21];
}completion:^(BOOL finished){
if(finished){
[button20 removeFromSuperview];
}
}];