How can i display string instead of image in UIView? - ios

Interface is define like this
#interface IGLDemoCustomView : UIView
#property (nonatomic, strong) UIImage *image;
#property (nonatomic, strong) NSString *title;
#end
While Implementation file look like this
#interface IGLDemoCustomView ()
#property (nonatomic, strong) UIImageView *imageView;
#property (nonatomic, strong) UILabel *titleLabel
#end
#implementation IGLDemoCustomView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit
{
[self initView];
}
- (void)initView
{
self.layer.borderColor = [UIColor colorWithRed:0.18 green:0.59 blue:0.69 alpha:1.0].CGColor;
self.layer.borderWidth = 2.0;
self.layer.masksToBounds = YES;
self.backgroundColor = [UIColor whiteColor];
self.alpha = 0.8;
UIImageView *imageView = [[UIImageView alloc] init];
imageView.contentMode = UIViewContentModeCenter;
[self addSubview:imageView];
self.imageView = imageView;
UILabel *titleLabel;
self.titleLabel = [[UILabel alloc]init];
titleLabel.center = self.center; // set proper frame for label
[self addSubview:titleLabel];
}
- (void)setImage:(UIImage *)image
{
_image = image;
self.imageView.image = image;
}
- (void)setString:(NSString *)title
{
self.title=title;
self.titleLabel.text = title;
}
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
self.imageView.frame = self.bounds;
self.layer.cornerRadius = frame.size.height / 2.0;
}
#end
When i select image from the drop down menu it shows in the menu, but when i select any text from the drop down menu it doesnt show in the drop down list view.
Any clue will be highly appreciated.
Calling and setting string in the view
IGLDropDownItem *menuButton = strongSelf.dropDownMenu.menuButton;
IGLDemoCustomView *buttonView = (IGLDemoCustomView*)menuButton.customView;
buttonView.title = device.name;

You need to set a frame for the label
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
self.imageView.frame = self.bounds;
self.titleLabel.frame = self.bounds;
self.layer.cornerRadius = frame.size.height / 2.0;
}
If you are expecting the text to wrap, you'll need to set the lineBreakMode and set numberOfLines to 0

Like you are having UIImageView you need to have a UILabel inside your UIView too.
#interface IGLDemoCustomView ()
#property (nonatomic, strong) UIImageView *imageView;
#property (nonatomic, strong) UILabel *titleLabel;
#end
- (void)initView
{
self.layer.borderColor = [UIColor colorWithRed:0.18 green:0.59 blue:0.69 alpha:1.0].CGColor;
self.layer.borderWidth = 2.0;
self.layer.masksToBounds = YES;
self.backgroundColor = [UIColor whiteColor];
self.alpha = 0.8;
UIImageView *imageView = [[UIImageView alloc] init];
imageView.contentMode = UIViewContentModeCenter;
[self addSubview:imageView];
self.imageView = imageView;
self.titleLabel = [[UILabel alloc]init];
titleLabel.center = self.center; // set proper frame for label
[self addSubview:titleLabel]
}
And in setString
- (void)setString:(NSString *)title
{
self.title=title;
self.titleLabel.text = title;
}

Related

My custom UIButton can't be used to replace leftBarButtonItem

I created a custom UIButton subclassing from UIControl (It is called "OZCustomButton"). It works fine in Storyboard, however when I was trying to use it to replace the leftBarButtonItem back button programmatically, it had a problem with its layout.
Here is the code I use to replace the leftBarButtonItem.
OZCustomButton *customBackButton = [[OZCustomButton alloc] initWithFrame:CGRectZero];
customBackButton.buttonImage = [UIImage imageNamed:#"BackArrow"];
customBackButton.buttonText = #"Back";
[customBackButton sizeToFit];
NSLog(#"this size: %#", NSStringFromCGRect(customBackButton.frame));
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:customBackButton];
self.navigationItem.leftBarButtonItem = item;
But nothing is showing on the left side of the navigation bar.
Navigation bar:
And the Dubug View Hierarchy tool shows these warning messages:
It seems the customBackButton is in the View Hierarchy, but the layout is not correct.
This is the code for my OZCustomButton.
OZCustomButton.h:
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
IB_DESIGNABLE
#interface OZCustomButton : UIControl <NSCoding>
#property (assign, nonatomic) IBInspectable CGFloat borderWidth;
#property (assign, nonatomic) IBInspectable CGFloat borderRadius;
#property (strong, nonatomic) IBInspectable UIColor *borderColor;
#property (strong, nonatomic) IBInspectable UIColor *fillColor;
#property (strong, nonatomic) IBInspectable UIColor *tintColor;
#property (strong, nonatomic) IBInspectable NSString *buttonText;
#property (assign, nonatomic) IBInspectable CGFloat textSize;
#property (assign, nonatomic) IBInspectable BOOL isTextBold;
#property (strong, nonatomic) UIFont *textFont;
#property (nullable, strong, nonatomic) IBInspectable UIImage *buttonImage;
#property (nullable, strong, nonatomic) NSArray *gradientColors;
#end
NS_ASSUME_NONNULL_END
OZCustomButton.m
#import "OZCustomButton.h"
#import "UIColor+Custom.h"
#import "CAGradientLayer+Utilities.h"
#interface OZCustomButton ()
#property (strong, nonatomic) UILabel *buttonLabel;
#property (nullable, strong, nonatomic) UIImageView *buttonImageView;
#property (nullable, strong, nonatomic) CAGradientLayer *gradientLayer;
#property (nullable, strong, nonatomic) UIStackView *stackView;
#end
#implementation OZCustomButton
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setupDefaults];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setupDefaults];
}
return self;
}
- (void)layoutLabelAndImageView {
_buttonLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_buttonLabel.numberOfLines = 1; // need to set to 1
_buttonLabel.textAlignment = NSTextAlignmentCenter;
//_buttonLabel.backgroundColor = [UIColor redColor];
_buttonImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
_buttonImageView.contentMode = UIViewContentModeScaleAspectFit;
//_buttonImageView.backgroundColor = [UIColor redColor];
_stackView = [[UIStackView alloc] init];
_stackView.axis = UILayoutConstraintAxisHorizontal;
_stackView.alignment = UIStackViewAlignmentCenter;
_stackView.distribution = UIStackViewDistributionFillProportionally;
_stackView.spacing = 8;
_stackView.userInteractionEnabled = NO;
[_stackView addArrangedSubview:_buttonImageView];
[_stackView addArrangedSubview:_buttonLabel];
_stackView.translatesAutoresizingMaskIntoConstraints = false;
[self addSubview:_stackView];
[[_stackView.centerXAnchor constraintEqualToAnchor:self.centerXAnchor] setActive:YES];
[[_stackView.centerYAnchor constraintEqualToAnchor:self.centerYAnchor] setActive:YES];
}
- (void)layoutGradientLayer {
_gradientLayer = [CAGradientLayer createGradientLayerWithBounds:self.bounds
colors:nil
direction:GradientFromLeftTopToRightBottom
locations:#[#0.0, #1.0]];
_gradientLayer.anchorPoint = CGPointMake(0, 0);
[self.layer insertSublayer:_gradientLayer below:_stackView.layer];
}
- (void)setupDefaults {
_borderWidth = 0.0f;
_borderRadius = 0.0f;
_borderColor = [UIColor blackColor];
_fillColor = [UIColor whiteColor];
_buttonText = #"Button";
_tintColor = [UIColor blackColor];
_textSize = 17.0f;
_isTextBold = false;
_textFont = _isTextBold ? [UIFont fontWithName:#"AlteHaasGrotesk_Bold" size:_textSize] : [UIFont fontWithName:#"AlteHaasGrotesk" size:_textSize];
_gradientColors = nil;
_buttonImage = nil;
[self layoutLabelAndImageView];
[self updateView];
}
- (void)updateView {
self.layer.borderColor = _borderColor.CGColor;
self.layer.borderWidth = _borderWidth;
self.layer.cornerRadius = _borderRadius;
self.layer.masksToBounds = true;
self.layer.backgroundColor = _fillColor.CGColor;
// update button text label
_buttonLabel.text = _buttonText;
_buttonLabel.textColor = _tintColor;
_textFont = _isTextBold ? [UIFont fontWithName:#"AlteHaasGrotesk_Bold" size:_textSize] : [UIFont fontWithName:#"AlteHaasGrotesk" size:_textSize];
_buttonLabel.font = _textFont;
_buttonLabel.textAlignment = NSTextAlignmentCenter;
[_buttonLabel sizeToFit];
// update button image
if (_buttonImage != nil) {
_buttonImageView.hidden = NO;
_buttonImageView.image = [_buttonImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
_buttonImageView.tintColor = _tintColor;
[_buttonImageView sizeToFit];
} else {
_buttonImageView.image = nil;
_buttonImageView.hidden = YES;
[_buttonImageView sizeToFit];
}
// update gradient layer
if (_gradientColors != nil) {
// if gradient layer is not initialized, call layoutGradientLayer()
if (_gradientLayer == nil) {
[self layoutGradientLayer];
}
// transform the UIColor to CGColorRef
NSMutableArray *colors = [NSMutableArray arrayWithCapacity:_gradientColors.count];
for (UIColor *color in _gradientColors) {
[colors addObject:(id)[color CGColor]];
}
if (colors.count > 0) {
_gradientLayer.colors = [colors copy];
}
}
}
#pragma mark - setters
- (void)setTextSize:(CGFloat)textSize {
if (_textSize != textSize) {
_textSize = textSize;
_textFont = _isTextBold ? [UIFont fontWithName:#"AlteHaasGrotesk_Bold" size:_textSize] : [UIFont fontWithName:#"AlteHaasGrotesk" size:_textSize];
[self updateView];
}
}
- (void)setBorderColor:(UIColor *)borderColor {
if (_borderColor != borderColor) {
_borderColor = borderColor;
[self updateView];
}
}
- (void)setBorderWidth:(CGFloat)borderWidth {
if (_borderWidth != borderWidth) {
_borderWidth = borderWidth;
[self updateView];
}
}
- (void)setBorderRadius:(CGFloat)borderRadius {
if (_borderRadius != borderRadius) {
_borderRadius = borderRadius;
[self updateView];
}
}
- (void)setFillColor:(UIColor *)fillColor {
if (_fillColor != fillColor) {
_fillColor = fillColor;
[self updateView];
}
}
- (void)setTintColor:(UIColor *)tintColor {
if (_tintColor != tintColor) {
_tintColor = tintColor;
[self updateView];
}
}
- (void)setButtonText:(NSString *)buttonText {
if (_buttonText != buttonText) {
_buttonText = buttonText;
[self updateView];
}
}
- (void)setTextFont:(UIFont *)textFont {
if (_textFont != textFont) {
_textFont = textFont;
[self updateView];
}
}
- (void)setGradientColors:(NSArray *)gradientColors {
if (_gradientColors != gradientColors) {
_gradientColors = gradientColors;
[self updateView];
}
}
- (void)setButtonImage:(UIImage *)buttonImage {
if (_buttonImage != buttonImage) {
_buttonImage = buttonImage;
[self updateView];
}
}
- (void)setIsTextBold:(BOOL)isTextBold {
if (_isTextBold != isTextBold) {
_isTextBold = isTextBold;
[self updateView];
}
}
#pragma mark - UIControl actions
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
CABasicAnimation *fadeAnimation = [CABasicAnimation animationWithKeyPath:#"opacity"];
fadeAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
fadeAnimation.duration = 0.2f;
if (highlighted) {
fadeAnimation.toValue = #0.6f;
} else {
fadeAnimation.toValue = #1.0f;
}
self.buttonLabel.layer.opacity = [fadeAnimation.toValue floatValue];
self.layer.opacity = [fadeAnimation.toValue floatValue];
[self.buttonLabel.layer addAnimation:fadeAnimation forKey:#"textFadeAnimation"];
[self.layer addAnimation:fadeAnimation forKey:#"backgroundFadeAnimation"];
}
- (void)setEnabled:(BOOL)enabled {
[super setEnabled:enabled];
if (enabled) {
self.layer.backgroundColor = self.fillColor.CGColor;
self.buttonLabel.textColor = self.tintColor;
} else {
self.layer.backgroundColor = [UIColor lighterColorForColor:self.fillColor].CGColor;
self.buttonLabel.textColor = [UIColor lightGrayColor];
}
}
#pragma mark - Override functions
- (void)layoutSubviews {
[super layoutSubviews];
[self updateView];
}
- (CGSize)sizeThatFits:(CGSize)size {
CGFloat minWidth = _buttonImageView.frame.size.width + 8 + _buttonLabel.frame.size.width;
CGFloat minHeight = MAX(_buttonImageView.frame.size.height, _buttonLabel.frame.size.height);
return CGSizeMake(minWidth + 6, minHeight + 4);
}
#end
I found one solution. Instead of setting the centenX anchor and centerY anchor, I change it to using left, right, top and bottom anchors:
[[_stackView.topAnchor constraintEqualToAnchor:_stackView.superview.topAnchor] setActive:YES];
[[_stackView.bottomAnchor constraintEqualToAnchor:_stackView.superview.bottomAnchor] setActive:YES];
[[_stackView.leftAnchor constraintEqualToAnchor:_stackView.superview.leftAnchor] setActive:YES];
[[_stackView.rightAnchor constraintEqualToAnchor:_stackView.superview.rightAnchor] setActive:YES];
Then it got the layout correctly.
If you have any other methods to solve the problem, please let me know.

Make Slides to go to next Page

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];
}}

Adding UIButton elements to UIView programmatically do not show up

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.

how to create bottom line border in multiple textfield

I'm just beginner in Objective C.
I want to create multiple textfield with only bottom line border.
for this i crete a new class customTextField
look at my code;-
CustomTextField.m
- (id)init {
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor grayColor].CGColor;
border.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, self.frame.size.height);
border.borderWidth = borderWidth;
[self.layer addSublayer:border];
self.layer.masksToBounds = YES;
return self;
}
in my loginViewController.h
#import <UIKit/UIKit.h>
#import "CustomTextField.h"
#interface LoginViewController : UIViewController
#property (strong, nonatomic) IBOutlet CustomTextField *txtFirstName;
#property (strong, nonatomic) IBOutlet CustomTextField *txtLastName;
#property (strong, nonatomic) IBOutlet CustomTextField *txtEmail;
#property (strong, nonatomic) IBOutlet CustomTextField *txtPassword;
#end
loginViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
_txtFirstName = [[CustomTextField alloc]init];
_txtLastName = [[CustomTextField alloc]init];
_txtEmail = [[CustomTextField alloc]init];
_txtPassword = [[CustomTextField alloc]init];
}
I don't know where I do mistakes. this code is doing nothing. Can any one tell me where I do wrong ?
any help or any suggestion.
Try with this:
self.layer.masksToBounds = YES;
self.layer.borderColor = [UIColor grayColor].CGColor;
self.layer.borderWidth = borderWidth;
But you're using IBOulet, which means your objects are initialised from the storyboard, so then you should move your code to awakeFromNib
- (void)awakeFromNib
{
self.layer.masksToBounds = YES;
self.layer.borderColor = [UIColor grayColor].CGColor;
self.layer.borderWidth = borderWidth;
}
Add a private border layer property in your .m file
#property (strong, nonatomic) IBOutlet CALayer *borderLayer;
Override layoutSubviews to set up the border.
- (void)layoutSubViews {
[super layoutSubViews];
if (self.borderLayer) {
self.borderLayer = [CALayer layer];
[self.layer addSublayer:border];
}
CGFloat borderWidth = 5.0f;
self.borderLayer.backgroundColor = color.CGColor;
self.borderLayer.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, borderWidth);
}
Update:
Don't initialize the CustomTextField IBOutlets in viewDidLoad. Simply set their class to CustomTextField in the storyboard!
you are using subclass with storyboard your code will fine if you create all button programatically but if you want that your code will work fine with story board and programatically then you have to use this code in your CustomTextField.m
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
[self initialize];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder])) {
// Initialization code
}
return self;
}
- (void) awakeFromNib
{
[super awakeFromNib];
}
- (void) layoutSubviews
{
for (UIView *view in self.subviews) {
[view removeFromSuperview];
}
[self initialize];
}
- (instancetype)init
{
self = [super init];
if (self) {
self = [self initWithFrame:self.frame];
}
return self;
}
-(void)initialize
{
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor grayColor].CGColor;
border.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, self.frame.size.height);
border.borderWidth = borderWidth;
[self.layer addSublayer:border];
self.layer.masksToBounds = YES;
}

UIImageView in UIScrollview is too big

I want to show an image using a UIImageView inside a UIScrollview. The image needs to fit on the screen while keeping the aspect ratio.
This all works, except that, when I zoom first, I can scroll below the image. The height of area is about 64.
This is how it looks after scrolling (White is the background color of the UIImageView and red is the background color of the UIScrollView):
This is the code in my ImageViewController:
#import "ImageViewController.h"
#interface ImageViewController () <UIScrollViewDelegate>
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#property (nonatomic, strong) UIImageView *imageView;
#property (nonatomic, strong) UIImage *image;
#end
#implementation ImageViewController
- (void)viewDidLoad
{
NSLog(#"viewDidLoad");
[super viewDidLoad];
_scrollView.minimumZoomScale = 1.0 ;
_scrollView.maximumZoomScale = _imageView.image.size.width / _scrollView.frame.size.width;
_scrollView.zoomScale = 1.0;
_scrollView.backgroundColor = [UIColor redColor];
_scrollView.delegate = self;
_imageView.backgroundColor = [UIColor whiteColor];
[_scrollView addSubview:_imageView];
}
- (void)viewDidLayoutSubviews
{
self.imageView.frame = self.scrollView.bounds;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
}
- (UIImageView *)imageView
{
if (!_imageView) _imageView = [[UIImageView alloc] init];
return _imageView;
}
- (UIImage *)image
{
NSLog(#"image");
return self.imageView.image;
}
- (void)setImage:(UIImage *)image
{
NSLog(#"setImage");
self.imageView.image = image;
}
#pragma mark - UIScrollViewDelegate
// mandatory zooming method in UIScrollViewDelegate protocol
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
-(void)setImageURL:(NSURL *)imageURL
{
NSLog(#"setImageURL");
_imageURL = imageURL;
self.image = [UIImage imageWithContentsOfFile:[imageURL path]];
}
#end
This are the properties of my UIScrollView:
How can I fix this?
I got it working. This is my code:
#import "ImageViewController.h"
#interface ImageViewController () <UIScrollViewDelegate>
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#property (nonatomic, strong) UIImageView *imageView;
#property (nonatomic, strong) UIImage *image;
#end
#implementation ImageViewController
- (void)viewDidLoad
{
NSLog(#"viewDidLoad");
[super viewDidLoad];
self.scrollView.contentSize = self.scrollView.frame.size;
self.scrollView.delegate = self;
CGRect rect = CGRectZero;
rect.size = self.image.size;
self.scrollView.minimumZoomScale = 0.5;
self.scrollView.maximumZoomScale = 2.5;
self.scrollView.zoomScale = 1.0;
self.imageView = [[UIImageView alloc] initWithFrame:rect];
self.imageView.image = self.image;
[self.scrollView addSubview:self.imageView];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
}
- (IBAction)tappedAction:(id)sender {
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:#[self.image] applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
}
- (void)viewDidLayoutSubviews
{
self.imageView.frame = self.scrollView.bounds;
}
#pragma mark - UIScrollViewDelegate
// mandatory zooming method in UIScrollViewDelegate protocol
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
UIView *subView = [scrollView.subviews objectAtIndex:0];
CGFloat offsetX = MAX((scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5, 0.0);
CGFloat offsetY = MAX((scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5, 0.0);
subView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,
scrollView.contentSize.height * 0.5 + offsetY);
}
-(void)setImageURL:(NSURL *)imageURL
{
NSLog(#"setImageURL");
_imageURL = imageURL;
self.image = [UIImage imageWithContentsOfFile:[imageURL path]];
}
#end
The code in scrollViewDidZoom: is used to keep the image centered.

Resources