My application starts by showing some Intro slides. 4 in total and in the last one, a button is used to close the slides and to go to the main page.
What I wish to make is to remove the button, and to make the last slide slidable. The user continues to slide to the right, and at the end it lands to the main page.
But don't know how to acheave that.
Here is the definition of
#interface IntroSlideViewController ()<ABCIntroViewDelegate>
#property ABCIntroView *introView;
#end
#implementation IntroSlideViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}
-(void)viewDidAppear:(BOOL)animated{
/***************Page Control**************/
self.introView = [[ABCIntroView alloc] initWithFrame:self.view.frame];
self.introView.delegate = self;
self.introView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1.000];
[self.view addSubview:self.introView];
/***************************************/
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)onDoneButtonPressed {
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:FIRSTTIME_LOGIN];
InitScreenViewController* initVc = [self.storyboard instantiateViewControllerWithIdentifier:#"initScreenViewController"];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:initVc];
[navVC setNavigationBarHidden:true];
[UIView transitionWithView:[AppDelegate getShareInstance].window duration:0.1f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
[AppDelegate getShareInstance].window.rootViewController = navVC;
}completion:nil];
}
#end
And the parent class ABCIntroViewDelegate:
#interface ABCIntroView () <UIScrollViewDelegate>
#property (strong, nonatomic) UIScrollView *scrollView;
#property (strong, nonatomic) UIPageControl *pageControl;
#property (strong, nonatomic) UIButton *doneButton;
#property (strong, nonatomic) UIView *viewOne;
#property (strong, nonatomic) UIView *viewTwo;
#property (strong, nonatomic) UIView *viewThree;
#property (strong, nonatomic) UIView *viewFour;
#property (strong, nonatomic) UIView *viewFive;
#end
#implementation ABCIntroView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
[self addSubview:self.scrollView];
[self addSubview:self.pageControl];
[self.scrollView addSubview:self.viewOne];
[self.scrollView addSubview:self.viewTwo];
[self.scrollView addSubview:self.viewThree];
[self.scrollView addSubview:self.viewFour];
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
//Done Button
//[self addSubview:self.doneButton];
}
return self;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = CGRectGetWidth(self.bounds);
CGFloat pageFraction = self.scrollView.contentOffset.x / pageWidth;
self.pageControl.currentPage = roundf(pageFraction);
}
-(UIView *)viewOne {
if (!_viewOne) {
_viewOne = [[UIView alloc] initWithFrame:self.frame];
UIImageView *imageview;
imageview = [[UIImageView alloc] initWithFrame:self.frame];
imageview.image = [UIImage imageNamed:#"intro_slide_0"];
[_viewOne addSubview:imageview];
}
return _viewOne;
}
-(UIView *)viewTwo {
if (!_viewTwo) {
CGFloat originWidth = self.frame.size.width;
CGFloat originHeight = self.frame.size.height;
_viewTwo = [[UIView alloc] initWithFrame:CGRectMake(originWidth, 0, originWidth, originHeight)];
UIImageView *imageviewTwo;
imageviewTwo = [[UIImageView alloc] initWithFrame:self.frame];
imageviewTwo.image = [UIImage imageNamed:#"intro_slide_1"];
[_viewTwo addSubview:imageviewTwo];
}
return _viewTwo;
}
-(UIView *)viewThree{
if (!_viewThree) {
CGFloat originWidth = self.frame.size.width;
CGFloat originHeight = self.frame.size.height;
_viewThree = [[UIView alloc] initWithFrame:CGRectMake(originWidth*2, 0, originWidth, originHeight)];
UIImageView *imageviewThree;
imageviewThree = [[UIImageView alloc] initWithFrame:self.frame];
imageviewThree.image = [UIImage imageNamed:#"intro_slide_2"];
[_viewThree addSubview:imageviewThree];
}
return _viewThree;
}
-(UIView *)viewFour {
if (!_viewFour) {
CGFloat originWidth = self.frame.size.width;
CGFloat originHeight = self.frame.size.height;
_viewFour = [[UIView alloc] initWithFrame:CGRectMake(originWidth*3, 0, originWidth, originHeight)];
UIImageView *imageviewFour;
imageviewFour = [[UIImageView alloc] initWithFrame:self.frame];
imageviewFour.image = [UIImage imageNamed:#"intro_slide_3"];
[_viewFour addSubview:imageviewFour];
UIButton *btnDone = [[UIButton alloc] initWithFrame:CGRectMake(30, self.frame.size.height - 130, self.frame.size.width-60, 50)];
[btnDone setImage:[UIImage imageNamed:#"slide_3_button_unpressed"] forState:UIControlStateNormal];
[btnDone setImage:[UIImage imageNamed:#"slide_3_button_pressed"] forState:UIControlStateHighlighted];
[btnDone addTarget:self.delegate action:#selector(onDoneButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[_viewFour addSubview:btnDone];
}
return _viewFour;
}
-(UIView *)viewFive {
if (!_viewFive) {
}
return _viewFive;
}
-(UIScrollView *)scrollView {
if (!_scrollView) {
_scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
[_scrollView setDelegate:self];
[_scrollView setPagingEnabled:YES];
[_scrollView setContentSize:CGSizeMake(self.frame.size.width*numberofPage, self.scrollView.frame.size.height)];
[self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
return _scrollView;
}
-(UIPageControl *)pageControl {
if (!_pageControl) {
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height-20, self.frame.size.width, 10)];
//[_pageControl setCurrentPageIndicatorTintColor:UIColorFromRGBAlpha(252, 61, 136, 1)];
//[_pageControl setPageIndicatorTintColor:UIColorFromRGBAlpha(223, 227, 232, 1)];
[_pageControl setNumberOfPages:numberofPage];
}
return _pageControl;
}
-(UIButton *)doneButton {
if (!_doneButton) {
_doneButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.frame.size.height-60, self.frame.size.width, 100)];
[_doneButton setTintColor:[UIColor whiteColor]];
// [_doneButton.titleLabel setFont:FONT_Regular(17)];
[_doneButton setBackgroundColor:[UIColor colorWithRed:96/255.0 green:167/255.0 blue:23/255.0 alpha:1]];
[_doneButton addTarget:self.delegate action:#selector(onDoneButtonPressed) forControlEvents:UIControlEventTouchUpInside];
}
return _doneButton;
}
#end
Maybe what I am asking is not possible to be implemented.
Any clues and ideas are welcome
You can easily achieve your requirement by modifying ABCIntroView class. From ABCIntroView you are getting the callback as - (void)onDoneButtonPressed to IntroSlideViewController so there you are navigating to MainViewController.
So follow up these steps to achieve your requirement
Remove done button code from ABCIntroView.
Modify the below function.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat pageWidth = CGRectGetWidth(self.bounds);
CGFloat pageFraction = self.scrollView.contentOffset.x/pageWidth;
self.pageControl.currentPage = roundf(pageFraction);
if (self.delegate != nil && self.pageControl.currentPage == 4){
[self.delegate onDoneButtonPressed];
}}
Related
I have a UIImageView and a UIView (filled with smaller imageviews) all placed in a UIScrollView (for zooming).
To better help you understand, I have a map (ImageView) with fog(UIView with smaller imageviews) in the scrollview so I can zoom the map.
If I add the mapview and fogview to the self.view then the application works as it should, touching the fog at a point removes that fog image.
However, when I add the map and fog views to the scrollview nothing responds to touches.
I ultimately want the default behavior to be touching fog view will remove the fog at one point. Unless the user presses a button to zoom/pinch the view, which will zoom both the mapview and fogview.
The problem below is no touches are processed when I add the mapview and fogview to the scrollview. Currently there is no ability to press a button to pinch/zoom as I still don't have the default behavior I want.
Any help?
#interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate, UIGestureRecognizerDelegate, UIScrollViewDelegate>{
UIImage * fogImage;
NSUserDefaults * defaults;
bool canZoom;
}
#property (weak, nonatomic) IBOutlet UIButton *btnTakePicture;
#property (strong, nonatomic) UIButton * btnMenu;
#property (strong, nonatomic) UIButton * btnZoom;
#property (strong, nonatomic) UIScrollView* scrollView;
#property (strong, nonatomic) UIImageView *mapView;
#property (strong, nonatomic) UIView * fogView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
fogImage = [UIImage imageNamed:#"fog1.jpg"];
self.mapView = [[UIImageView alloc] initWithFrame:CGRectZero];
self.fogView = [[UIView alloc] initWithFrame:CGRectZero];
_scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
_scrollView.delegate = self;
_scrollView.minimumZoomScale = 0.75;
_scrollView.maximumZoomScale = 3.0;
[_scrollView addSubview:self.mapView];
[_scrollView addSubview:self.fogView];
[self.view addSubview:_scrollView];
self.mapView.hidden = YES;
self.fogView.hidden = YES;
self.scrollView.hidden = YES;
defaults = [NSUserDefaults standardUserDefaults];
canZoom = false;
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.mapView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
self.fogView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
self.scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
- (void) zoom:(id)sender{
canZoom = !canZoom;
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
self.btnTakePicture.hidden = YES;
self.btnMenu.hidden = NO;
self.btnZoom.hidden = NO;
[picker dismissViewControllerAnimated:YES completion:^(void) {
[self fillScreenWithFog];
self.mapView.image = chosenImage;
self.mapView.contentMode = UIViewContentModeScaleAspectFit;
self.mapView.hidden = NO;
self.fogView.hidden = NO;
_scrollView.hidden = NO;
_scrollView.userInteractionEnabled = YES;
self.scrollView.contentSize = self.mapView.frame.size;
}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark Fog Maker
- (void) removeFogAtPoint:(CGPoint)point{
NSLog(#"removeFogAtPoint %#", NSStringFromCGPoint(point));
CGRect fingerRect = CGRectMake(point.x - 5, point.y-5, 10, 10);
for(UIImageView *view in self.fogView.subviews){
CGRect subviewFrame = view.frame;
if(CGRectIntersectsRect(fingerRect, subviewFrame)){
[UIView animateWithDuration:1.25
animations:^{
view.alpha = 0;
}
completion:^(BOOL finished){
[view removeFromSuperview];
}];
}
}
}
- (void) fillScreenWithFog {
// do a loop to fill the screen with fog getNewSquare
}
- (UIImageView*) getNewSquare:(CGRect)frame withTag:(int)tag{
UIImageView * imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.image = fogImage;
imageView.contentMode = UIViewContentModeScaleAspectFill;
return imageView;
}
-(void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(#"Touches Moved");
CGPoint location = [[touches anyObject] locationInView:self.scrollView];
[self removeFogAtPoint:location];
}
#end
I have a custom class A that is a subclass of UIView which essentially manages a UIScrollView that fits the size of the view and the contains an UIImageView that fills that scrollView.
I am having trouble setting the imageView.image of the custom class:
Here's the class
#import "BackgroundPickerView.h"
#implementation BackgroundPickerView
#synthesize scrollView;
#synthesize imageView;
#synthesize actionButton;
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
//[[NSNotificationCenter defaultCenter] addObserver:self
// selector:#selector(changeImage)
// name:#"ImageChangeNotification"
// object:nil];
//Here's where we add custom subviews
scrollView = [[UIScrollView alloc] init];
imageView = [[UIImageView alloc] init];
actionButton = [[UIButton alloc] init];
[scrollView setFrame:CGRectMake(0, 0, 321, 115)];
[imageView setFrame:CGRectMake(0, 0, 321, 115)];
[actionButton setFrame:CGRectMake(0, 0, 320, 115)];
scrollView.scrollEnabled = YES;
scrollView.minimumZoomScale = 1.0;
scrollView.maximumZoomScale = 6.0;
scrollView.contentSize = CGSizeMake(300, 200);//imageView.image.size;
[scrollView addSubview:imageView];
[self addSubview:scrollView];
}
-(void)storeImage:(UIImage *)image
{
self.imageView.image = image;
}
-(void)changeImage
{
[self.imageView setImage:[UIImage imageNamed:#"random.png"]];
}
From my other class B where I receive the image I have tried using NSNotification and a simple setter method to try setting the imageView property of the class object, but cannot seem to set the imageView.image. I have tried using
instance.imageView.image = myImageToSet
//Setter
[instance storeImage:myImageToSet];
in class B but am having no success
first of all, the initialisation of subviews in layoutSubviews not the best way, better u initialise them in the init method, because layoutSubviews may call multiple times to layout the subviews, if u place the initialisation code in the layoutSubviews there might be may subviews. in layoutSubviews u only set the frame of the subviews.
and u can do like below,
#import "BackgroundPickerView.h"
#implementation CustomViewA
//Edit change below
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
[self commonInit];
}
return self;
}
- (void)awakeFromNib
{
[self commonInit];
}
- (void)commonInit
{
_scrollView = [[UIScrollView alloc] init];
_imageView = [[UIImageView alloc] init];
_actionButton = [[UIButton alloc] init];
_scrollView.scrollEnabled = YES;
_scrollView.minimumZoomScale = 1.0;
_scrollView.maximumZoomScale = 6.0;
[_scrollView addSubview:_imageView];
[self addSubview:_scrollView];
}
- (void)layoutSubviews
{
[super layoutSubviews];
[_scrollView setFrame:CGRectMake(0, 0, 321, 115)];
[_imageView setFrame:CGRectMake(0, 0, 321, 115)];
[_actionButton setFrame:CGRectMake(0, 0, 320, 115)];
_scrollView.contentSize = CGSizeMake(300, 200);
}
-(void)storeImage:(UIImage *)image
{
self.imageView.image = image;
}
-(void)changeImage
{
[self.imageView setImage:[UIImage imageNamed:#"random.png"]];
}
synthesise is optional
and in BackgroundPickerView.h file it is something like below
#interface BackgroundPickerView : UIView
#property (nonatomic, strong) UIScrollView *scrollView;
#property (nonatomic, strong) UIImageView *imageView;
#property (nonatomic, strong) UIButton *actionButton;
-(void)storeImage:(UIImage *)image;
-(void)changeImage;
for image u check it it should work, check the image is present or not,
Edit
For the image, in controller for example,
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_viewA = [[BackgroundPickerView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_viewA];
}
//for testing
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[_viewA storeImage:[UIImage imageNamed:#"6.png"]]; //hear do this
}
I am trying to add header and footer (both of them as UIViews) but for some reason my footer sticks to the bottom, I'm using the KVO method for watching my content size.
I'm presenting here the method where I think the problem is:
- (void)updateLayout
{
// Update the frame of the header view so that it scrolls with the webview content
CGRect newHeaderFrame = self.headerView.frame;
CGRect newFooterFrame = self.footerView.frame;
newHeaderFrame.origin.y = -CGRectGetMinY([self.webView convertRect:self.innerHeaderView.frame toView:self.webView.scrollView]);
newFooterFrame.origin.y = self.webView.scrollView.contentSize.height;
[self.headerView setFrame:newHeaderFrame];
[self.footerView setFrame:newFooterFrame];
if ([self didTapToFullScreen])
{
// The delegate was already called in this case, in the tap gesture callback method
return;
}
BOOL fullScreen = (newHeaderFrame.origin.y < 0);
if (([self isZooming] && [self didSwitchToFullScreen]) || (fullScreen == [self isFullScreen]))
{
return;
}
[self setSwitchToFullScreen:fullScreen];
[self setFullScreen:fullScreen];
// Call the delegate for the full screen
[self.fullScreenDelegate emailView:self showFullScreen:fullScreen];
}
Here's the full project
How I can place the footer to the bottom of the UIWebView ?
You can set the views on UIWebView scroll view, and fiddle with the offsets.
#interface ViewController ()<UIWebViewDelegate>
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#property (nonatomic, strong) UIView *headerView;
#property (nonatomic,strong) UIView *footerView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -100, [UIScreen mainScreen].bounds.size.height, 100)];
self.headerView.backgroundColor = [UIColor blueColor];
self.footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
self.footerView.backgroundColor = [UIColor yellowColor];
NSString *urlText = #"http://stackoverflow.com/questions/15921974/how-to-load-url-on-launch-in-a-webview-osx-project";
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
self.webView.delegate = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - WebView delegate
-(void) webViewDidFinishLoad:(UIWebView *)webView {
self.webView.scrollView.contentInset = UIEdgeInsetsMake(100.0,0.0,100.0,0.0);
if(![self.headerView superview])
{
[webView.scrollView addSubview:self.headerView];
[webView.scrollView bringSubviewToFront:self.headerView];
}
NSString *result = [webView stringByEvaluatingJavaScriptFromString:#"document.body.offsetHeight;"];
NSInteger height = [result integerValue];
self.footerView.frame = CGRectMake(0, height, [UIScreen mainScreen].bounds.size.height, 100);
if(![self.footerView superview])
{
[webView.scrollView addSubview:self.footerView];
[webView.scrollView bringSubviewToFront:self.footerView];
}
[self.webView.scrollView setContentOffset:
CGPointMake(0, -self.webView.scrollView.contentInset.top) animated:NO];
}
#end
Another option would be to add top padding to the HTML ("margin-top:100px") and then the header view would not be in minus offset.
I'm writing a custom UIView class that has its own sub UIView. I want this UIView to be centered inside the superView with two UIButtons inside of it, two UILabels.
I overwrite my init with the parameters I want in the custom UIView and in that init method in implementation, I am alloc/initializing all my objects and at the end of my init method, I add my subView to self. And I add all the buttons and objects to the subView.
The problem is, even after setting my coordinates and frames in layoutSubviews, my buttons and labels do not appear in the subView.
Here's my method code:
#property (nonatomic, weak) UIView *alertSubView; // this will be the message view in middle of screen
#property (nonatomic, weak) UILabel *alertTitleLabel; // This will be the title within the alertSubView
#property (nonatomic, weak) UILabel *alertMessageLabel; // This will be the alert message under the alert title
#property (nonatomic, weak) UIButton *doNotAskButton; // This will be the do not ask button
#property (nonatomic, weak) UIButton *cancelButton; // This will be the cancel button, bottom left of alertView
#property (nonatomic, weak) UIButton *confirmButton; // This will be the confirm button, bottom right of alertView
#property (nonatomic) BOOL doNotAsk;
#end
#implementation MyAlertView
- (instancetype)initWithFrame:(CGRect)frame messageTitle:(NSString *)title messageSubject:(NSString *)subject shouldAskForDoNotAsk:(BOOL)doNotAsk delegate:(id<MyAlertViewDelegate>)delegate
{
if (self = [super initWithFrame:frame]) {
self.delegate = delegate;
UILabel *alertLabel = [[UILabel alloc] init];
alertLabel.text = title;
alertLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:15];
self.alertTitleLabel = alertLabel;
UILabel *messageLabel = [[UILabel alloc] init];
messageLabel.text = subject;
alertLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:15];
self.alertMessageLabel = messageLabel;
self.backgroundColor = [UIColor colorWithRed:170.0f/255.0f green:170.0f/255.0f blue:170.0f/255.0f alpha:0.75];
UIView *alertBoxView = [[UIView alloc] init];
self.alertSubView = alertBoxView;
[self.alertSubView addSubview:self.alertTitleLabel];
[self.alertSubView addSubview:self.alertMessageLabel];
if (doNotAsk) {
UIButton *buttonDoNotAsk = [[UIButton alloc] init];
buttonDoNotAsk.titleLabel.text = #"Do not ask me again";
self.doNotAskButton = buttonDoNotAsk;
[self.alertSubView addSubview:self.doNotAskButton];
}
UIButton *cancelButton = [[UIButton alloc] init];
cancelButton.titleLabel.text = #"Cancel";
cancelButton.titleLabel.textColor = [UIColor blueColor];
cancelButton.backgroundColor = [UIColor whiteColor];
cancelButton.opaque = YES;
self.cancelButton = cancelButton;
[self.alertSubView addSubview:self.cancelButton];
UIButton *confirmButton = [[UIButton alloc] init];
confirmButton.titleLabel.text = #"OK";
self.confirmButton = confirmButton;
[self.alertSubView addSubview:self.confirmButton];
self.alertSubView.backgroundColor = [UIColor whiteColor];
[self addSubview:self.alertSubView];
}
return self;
}
- (void)layoutSubviews
{
// place the alertView in the center of self view
CGFloat alertHeight = kAlertHeightModifier * self.frame.size.height;
CGFloat alertWidth = kAlertWidthModifier * self.frame.size.width;
[self.alertSubView setFrame:CGRectMake(0, 0, alertWidth, alertHeight)];
[self.alertSubView addSubview:self.cancelButton];
[self setUpButtonsAndLabels];
[self.alertSubView setCenter:self.center];
}
- (void) setUpButtonsAndLabels {
CGFloat alertHeight = kAlertHeightModifier * self.frame.size.height;
CGFloat alertWidth = kAlertWidthModifier * self.frame.size.width;
CGFloat buttonWidth = 0.5 * alertWidth;
CGFloat buttonHeight = 0.2 * alertHeight;
[self.cancelButton setFrame:CGRectMake(15, 45, buttonWidth, buttonHeight)];
[self.confirmButton setFrame:CGRectMake(0, 0, buttonWidth, buttonHeight)];
[self.confirmButton setCenter:self.center];
}
Try replacing your - (void)layoutSubviews with this .
- (void)layoutSubviews
{
// place the alertView in the center of self view
CGFloat alertHeight = kAlertHeightModifier * self.frame.size.height;
CGFloat alertWidth = kAlertWidthModifier * self.frame.size.width;
[self.alertSubView setFrame:CGRectMake(0, 0, alertWidth, alertHeight)];
[self.alertSubView addSubview:self.cancelButton];
[self setUpButtonsAndLabels];
[self.alertSubView setCenter:self.center];
[super layoutSubviews];
}
Or else initialize the button in viewdidload .It will work .
Try doing it in viewDidLoad. Should help to eliminate the problem, because the view will be fully initialized and ready to use.
hi i'm creating my own customized ComboBox, i copy on here my code so that anyone can use it. ( i don't know where else to put it lol )
I'd like also to know if you have better idea to improve it.
I get a small error, when i init the instance with the initWithBlurredImage, everything works fine but the blurred image doesn't cover the whole screen, it misses a small frame of about 5px. can you understand why?
//
// UIComboBox.h
// testComboBox
//
// Created by StabiloCode on 09/09/13.
// Copyright (c) 2013 SimplyXcode. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface UIComboBox : UIViewController <UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource> {
UITextField *myTextField;
UIImageView *imageView;
UIPickerView *pickerView;
NSMutableArray *array;
UIToolbar *toolBar;
CGRect oldFrame;
UIImage *blurImage;
UIImageView *backgroundView;
}
#property (nonatomic, retain) UITextField *myTextField;
#property (nonatomic, retain) UIImageView *imageView;
#property (nonatomic, retain) UIPickerView *pickerView;
#property (nonatomic, retain) NSMutableArray *array;
#property (nonatomic, retain) UIImage *blurImage;
-(void)comboBox:(UIComboBox *)comboBox dataSource:(NSMutableArray *)anArray;
-(UIImage *)createBlur;
-(id)initwithBlurredImage:(UIImage *)anImage;
#end
//
// UIComboBox.m
// testComboBox
//
// Created by StabiloCode on 09/09/13.
// Copyright (c) 2013 SimplyXcode. All rights reserved.
//
#import "UIComboBox.h"
#interface UIComboBox ()
#end
#implementation UIComboBox
#synthesize myTextField, imageView, array, pickerView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
-(id)init {
self = [super init];
if ( self ){
}
return self;
}
-(id)initwithBlurredImage:(UIImage *)anImage{
blurImage = anImage;
NSLog(#"blur = %#",blurImage);
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
myTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 300, 30)];
[myTextField setBorderStyle:UITextBorderStyleRoundedRect];
[self.view addSubview:myTextField];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(myTextField.frame.size.width-20, myTextField.frame.size.height-17, 25, 25)];
UIImage *arrow = [UIImage imageNamed:#"arrow.png"];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.clipsToBounds = YES;
[imageView setImage:arrow];
[self.view addSubview:imageView];
[myTextField setDelegate:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
[self showPickerView];
}
-(void)showPickerView{
//saving old frame to fix the position after the selection
oldFrame = self.view.frame;
//changing the frame to have the whole screen for the comboBox
[self.view setFrame:CGRectMake(0, 0, 320, 460)];
if(blurImage){
backgroundView = [[UIImageView alloc] initWithImage:blurImage];
backgroundView.backgroundColor = [UIColor colorWithPatternImage:blurImage];
[backgroundView setFrame:CGRectMake(0,0, 320, 460)];
[backgroundView setContentMode:UIViewContentModeScaleToFill];
[self.view addSubview:backgroundView];
[self.view sendSubviewToBack:backgroundView];
}
if ([myTextField.text isEqual: #""]){
myTextField.text = [array objectAtIndex:0];
}
[myTextField setEnabled:NO];
//creating the pickerview
pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 244, 320, 216)];
[pickerView setDataSource:self];
[pickerView setDelegate:self];
[pickerView showsSelectionIndicator];
[pickerView setShowsSelectionIndicator:YES];
[self.view addSubview:pickerView];
toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 200, 320, 244)];
toolBar.barStyle = UIBarStyleBlackTranslucent;
[toolBar sizeToFit];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancel)];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar setItems:#[space,cancelButton, doneButton]];
[self.view addSubview:toolBar];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [array objectAtIndex:row];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1; }
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [array count];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
myTextField.text = [array objectAtIndex:row];
}
-(void)done{
[toolBar removeFromSuperview];
[pickerView removeFromSuperview];
[myTextField setEnabled:YES];
[self.view setFrame:oldFrame];
if(blurImage){ [backgroundView removeFromSuperview]; }
}
-(void)cancel{
myTextField.text = #"";
[toolBar removeFromSuperview];
[pickerView removeFromSuperview];
[myTextField setEnabled:YES];
[self.view setFrame:oldFrame];
if(blurImage){ [backgroundView removeFromSuperview]; }
}
// setting the pickerView content
-(void)comboBox:(UIComboBox *)comboBox dataSource:(NSMutableArray *)anArray{
self.array = anArray;
}
#end
if you want to use the code, you're free to do that, just let me know if it works fine :) stabilocode#gmail.com