change the color of scrollbar in tableview - ios

I have one viewcontroller.Inside that i placed one tableview with size 320X295 at centre of my viewcontroller. so my table view will be half size at centre in my viewcontroller.
My tableview is not fully fill my Viewcontroller.Now how can i change my scroll bar color to green color or some RGB Value.
Here is my code:
UIScrollView *scView = [[UIScrollView alloc] init];
scView.frame = self.view.bounds; //scroll view occupies full parent views
scView.contentSize = CGSizeMake(400, 400);
// scView.backgroundColor = [UIColor lightGrayColor];
scView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
scView.showsHorizontalScrollIndicator = NO;
scView.showsVerticalScrollIndicator = YES;
scView.scrollEnabled = YES;
[self.containerTableView addSubview: scView];
ContainerTableView is my tableview name.
i need to change my scroll bar color to green.Please help me how can i achive that.Thanks

1 .Add UIScrollViewDelegate
#interface ViewController : UIViewController<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
#end
2. Add scrollViewDidScroll in implementation section
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//get refrence of vertical indicator
UIImageView *verticalIndicator = ((UIImageView *)[scrollView.subviews objectAtIndex:(scrollView.subviews.count-1)]);
//set color to vertical indicator
[verticalIndicator setBackgroundColor:[UIColor redColor]]; // Your color
//get refrence of horizontal indicator
UIImageView *horizontalIndicator = ((UIImageView *)[scrollView.subviews objectAtIndex:(scrollView.subviews.count-2)]);
//set color to horizontal indicator
[horizontalIndicator setBackgroundColor:[UIColor blueColor]]; // Your color
}

You can achieve this by using the property of UIScrollView named indicatorStyle
Use any of the following
typedef enum {
UIScrollViewIndicatorStyleDefault,
UIScrollViewIndicatorStyleBlack,
UIScrollViewIndicatorStyleWhite
} UIScrollViewIndicatorStyle;
you can use this style like this.
tableView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
here is the link for reference..
Change width and colour of scroll bar in UITableView, iphone

Related

UIButton in a custom UIView is not being triggered when tapped upon

I've placed a UIButton inside a custom UIView, CustomButtonView. The UIButton is not responding to tap/click events, and its appropriate selector method is not being triggered. Here's my code below; what have I done wrong?
As well, the custom view has a background color of red. When I add the custom view to my view controller's view, the red rectangle displays. However, when I add my UIButton to my custom view (with the background color of red), only the UIButton displays, not the red rectangle. Why is this happening?
//
// CustomButtonView.h
//
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
#interface CustomButtonView : UIView
#property (strong, nonatomic) UIImageView *buttonImageView;
#property (strong, nonatomic) UIButton *button;
#end
NS_ASSUME_NONNULL_END
//
// CustomButtonView.m
//
#import "CustomButtonView.h"
#implementation CustomButtonView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor redColor];
_button = [UIButton buttonWithType:UIButtonTypeSystem];
_button.backgroundColor = [UIColor greenColor];
[_button setTitle:#"john doe" forState:UIControlStateNormal];
[_button setTitle:#"john doe" forState:UIControlStateHighlighted];
_button.translatesAutoresizingMaskIntoConstraints = NO;
_button.titleLabel.font = [UIFont boldSystemFontOfSize:14.0];
_button.titleLabel.textColor = [UIColor whiteColor];
self.userInteractionEnabled = NO;
self.button.userInteractionEnabled = YES;
//[self addSubview:_button];
//[_button.topAnchor constraintEqualToAnchor:self.topAnchor].active = YES;
//[_button.leadingAnchor constraintEqualToAnchor:self.leadingAnchor].active = YES;
}
return self;
}
#end
//
// ViewController.m
// TestApp
//
//
#import "ViewController.h"
#import "CustomButtonView.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
_customButtonView = [[CustomButtonView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
_customButtonView.backgroundColor = [UIColor redColor];
_customButtonView.translatesAutoresizingMaskIntoConstraints = NO;
[_customButtonView.button addTarget:self action:#selector(customButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_customButtonView];
}
- (void)customButtonTapped:(id)sender{
NSLog(#"Button tapped!");
}
#end
Here are some screenshots, below. Please click on the links.
When I display the UIButton inside the custom-view, only the button displays. The red rectangle of the custom-view doesn't display.
When I simply add the custom-view to my view-controller (ie with no UIButton added to the custom view), the red rectangle of the custom-view displays.
When you disable user interaction for a specific view then this user interaction is not forwarded to its subviews. Or in other words; for user interaction to work you need to make sure that all superviews in chain (superview of superview of superview) do have user interaction enabled.
In your case you have self.userInteractionEnabled = NO; which means that any subview of self (including your button) will not accept any user interaction.
Next to this issues it is still possible that other issues are present. For instance: A subview will accept user interaction only within physical area of all of its superviews. That means that if a button with frame { 0, 0, 100, 100 } is placed on a superview of size { 100, 50 } then only the top part of the button will be clickable.
As for the size changes this is a completely different story...
You decided to disable translation of autoresizing mask on your custom view by writing _customButtonView.translatesAutoresizingMaskIntoConstraints = NO;. That means that you will not use frame in conjunction with other constraints. It means that whatever frame you set (in your case CGRectMake(0, 0, 100, 100)) this frame will be overridden by anything else that may effect the frame of your custom view.
In case where your button code is commented out there is no such thing that "may effect the frame of your custom view" and the view stays as size of { 100, 100 }. But as soon as you added a button you added additional constraints which most likely lead to resizing your button to sizeToFit and with it its superview which is your custom view.
To avoid this you need to either remove disabling of translation of autoresizing mask into constraint on your custom view. Or you need to define custom view size by adding constraints to it so that they are set to { 100, 100 }. You can simply constrain heightAnchor and widthAnchor to 100.

How to make top navigation controller clickable?

I have the following storyboard. Navigating to Class VC, I need to create custom buttons at the top of the VC and I think it will be beneath the Top Navigation Bar Controller. How do I make my custom buttons on top of the Navigation Bar Controller and clickable. I have already make the Top Navigation Bar appearance to be transparent.
I have the following code that is working and the layout is at the following pic However this is not my desired layout. It is located somewhere below the Transparent Navigation Bar controller area.
//--- customizeable button attributes
CGFloat X_BUFFER = 0.0; //--- the number of pixels on either side of the segment
CGFloat Y_BUFFER = 0.0; //--- number of pixels on top of the segment
CGFloat HEIGHT = 50.0; //--- height of the segment
//--- customizeable selector bar attributes (the black bar under the buttons)
CGFloat SELECTOR_Y_BUFFER = 0.0; //--- the y-value of the bar that shows what page you are on (0 is the top)
CGFloat SELECTOR_HEIGHT = 44.0; //--- thickness of the selector bar
CGFloat X_OFFSET = 8.0; //--- for some reason there's a little bit of a glitchy offset. I'm going to look for a better workaround in the future
CGFloat numControllers = 7.0;
-(void)setupSegmentButtons
{
navigationView = [[UIView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,52)];
navigationView.backgroundColor = [UIColor greenColor];
[self.view addSubview:navigationView];
//——Format Some Dates
for (int i = 0; i<numControllers; i++) {
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(i*(self.view.frame.size.width/numControllers), Y_BUFFER, (self.view.frame.size.width/numControllers),HEIGHT)];
[navigationView addSubview:button];
NSString *lblDate = [datelblFormat stringFromDate:[calendar dateFromComponents:comps]];
UILabel *firstLineButton = [[UILabel alloc] initWithFrame:CGRectMake(-4,0,self.view.frame.size.width/numControllers,30)];
firstLineButton.text = lblDate;
[button addSubview:firstLineButton];
NSString *lblDay = [daylblFormat stringFromDate:[calendar dateFromComponents:comps]];
UILabel *secondLineButton = [[UILabel alloc] initWithFrame:CGRectMake(-4,30,self.view.frame.size.width/numControllers,15)];
secondLineButton.text = lblDay;
[button addSubview:secondLineButton];
button.tag = i; //--- IMPORTANT: if you make your own custom buttons, you have to tag them appropriately
button.backgroundColor = [UIColor brownColor];//— buttoncolors
[button addTarget:self action:#selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
++comps.day;
}
[self setupSelector];
}
//--- sets up the selection bar under the buttons on the navigation bar
-(void)setupSelector {
selectionBar = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/numControllers, Y_BUFFER, (self.view.frame.size.width/numControllers),HEIGHT)];
selectionBar.backgroundColor = [UIColor colorWithRed:189.0f/255.0f green:225.0f/255.0f blue:255.0f/255.0f alpha:0.6];
[navigationView addSubview:selectionBar];
}
I manage to move the custom buttons to the top by changing the Y Coordinates and I got the following results. Its not ideal cause I need the brown colour to be all the way to the edge of the phone especially iPhoneX and the buttons to be clickable.
I was not able to comment that's why suggesting this as an answer.
I think you should present your view controller instead of pushing it.
[self presentViewController:#"your view controller" animated:YES completion:nil];
Answer:
To hide the navigation bar at the current VC or the 1st VC
-(void)viewWillAppear:(BOOL)animated{
[[self navigationController] setNavigationBarHidden:YES animated:YES];
}
To UNHIDE the navigation bar on subsequent VC or the 2nd VC. At the 1st VC, apply the following code.
-(void)viewDidDisappear:(BOOL)animated{
[[self navigationController] setNavigationBarHidden:NO animated:YES];
}
Then the buttons will be clickable.

Scrolldown a TableView makes the UIImage background move up

Hi here is what I'm trying to do.
I've got a Table View with transparent cell's background.
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"trans.png"]];
And I added to the ViewDidLoad a background image.
- (void)viewDidLoad {
bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"homelights.png"]];
[super viewDidLoad];
self.title = #"";
}
So this works fine, I've got a tableView with transparent cells and a background image. Now the plan is when I scroll the TableView down, the image should react and move up.
Any idea how to trigger the scrolldown? Or where to start?
I enabled scrollViewDidScroll;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat scrollOffset = scrollView.contentOffset.y;
Works like a charm, I can read the scroll position, but when I'm trying to animate UIView, by linking the scrollView.contentOffset.y to the UIView "y" position;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat scrollOffset = scrollView.contentOffset.y;
[UIView beginAnimations:#"UIViewAnimationCurveEaseInOut" context:nil];
bgView.center = CGPointMake(bgView.center.x, bgView.center.y+scrollOffset);
[UIView commitAnimations];
}
So I tried to make the UIView bigger to the original size of the image 640x832 to be able to navigate on it, but doesn't seems to work.
bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 640, 832)];
bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"homelights.png"]];
Any idea why this is happening?
Your table view is a subclass of UIScrollView and your delegate for the table view is also (automatically) a delegate of the scroll view. Implement the scrollViewDidScroll: delegate method and use it to monitor how the table view has changed and move your background view.

UIImageView on top and set size

1) I have a UIView with a UIImageView on it, i load pictures from the image gallery. I want to know how I would "center" the picture on the screen and make that the aspect ratio isn't changed. For instance if I load a picture that is on landscape mode, how do I make sure that the picture isn't stretched all over the screen? My app is working in portrait mode.
2) In that same view I want to add the effect that when the user touches the screen the botton menu fades out and back in when he presses again. I'm not sure what that is called and I couldn't get the tap gesture recognizer to work.
EDIT:
I set the UIImageView on page load
- (void)viewDidLoad
{
[super viewDidLoad];
xlabel = [[UILabel alloc] initWithFrame:self.setLablePosition];
xlabel.backgroundColor = [UIColor clearColor];
imgView.image = self.appDelegate.theImg; // now the image is put on a UIImageView that is "full screen"
xlabel.text = #"Some text";
[imgView addSubview:xlabel];
// Do any additional setup after loading the view.
}
and as for Tap gesture :
-(IBAction)screenTapped{
NSLog(#"screen Tapped");
}
the IBAction is linked to the tap gesture recognizer.
For centering the UIImageView in the view you would do the following
//First give imgView the correct size
//You may need to divide the size by some constant if the image is too big
imgView.frame = CGRectMake(0, 0, self.appDelegate.theImg.size.width, self.appDelegate.theImg.size.width);
//Then center it
imgView.center = self.view.center;
For adding the tap gesture recognizer do the following
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(screenTapped)];
//By deafualt interaction is disabled
imgView.userInteractionEnabled = YES;
[imgView addGestureRecognizer:recognizer];
My solution was
1) add imgView.contentMode = UIViewContentModeScaleAspectFit;
2) like Omar Abdelhafith said i added imgView.userInteractionEnabled = YES;, the UITapGestureRecognizer for me was created it the storyboard.

iOS UIScrollView in UIView

I have a little bit specific question. It might not matter for most people but I have had to deal with it and I had to solve the issue described below. I tried to find some information about it using Google and the Apple SDK documentation but did not succeed.
I was a designing a screen where there were many images in horizontal scrolls. There three three same scrolls. Every scroll had title. I have implemented custom class derived from UIView and placed there UIScrollView for scroll and UILabel for title text:
#interface MyView : UIView {
UIScrollView *iScrollView;
UIView *iTitleView;
}
I then put objects of this class on the view of a UIViewController:
#implementation MyViewController
- (void) viewDidLoad {
[super viewDidLoad];
...
iScrollViewTop = [[MyView alloc] init];
[self.view addSubview:iScrollViewTop];
...
}
#end
When I filled the internal scroll view with images and ran my application it looked OK. But there was some strange behavior. First, scroll did not have bounces as if I had set
iScrollView.bounces = NO;
and second, when I swiped to scroll, after the scroll stopped, the scroll bar did not disappear within one second. It was strange for me, because when I usually create a UIScrollView and add it to the UIViewController's view it has bounces and scroll bar disappears immediately when it stops. I tried to change UIScrollView's properties, such as directionalLockEnabled, pagingEnabled, canCancelContentTouches, delaysContentTouches, decelerationRate and others. In fact, I have tried to change almost all properties of UIScrollView but I could not get the scroll bars to immediately disappear.
If I try to add UIScrollView instead MyView to the UIViewController.view, it bounces and scroll bar disappears immediately after it stops. Also I get correct behavior if I subclass MyView from UIScrollView but in this case I cannot manage the title label because it scrolls together with other content.
So here are my questions:
Do you know why I am seeing this behavior?
How can I get "usual" behavior for scroll encapsulated by UIView?
ok, hacky code follows, so ignore all my other issues, but follow this pattern (westie.jpg = image that was 360x200)
#interface MyView : UIView
{
UIScrollView *sv;
UILabel *l;
}
-(MyView*)initWithFrame:(CGRect)frame;
#end
#implementation MyView
-(MyView*)initWithFrame:(CGRect)frame;
{
self = [super initWithFrame:frame];
sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,360,200)];
sv.scrollEnabled = YES;
sv.contentSize = CGSizeMake(360*3,200);
[self addSubview:sv];
UIImage *i1 = [UIImage imageNamed:#"westie.jpg"];
UIImageView *iv1 = [[UIImageView alloc] initWithImage:i1];
iv1.frame = CGRectMake(360*0, 0, 360, 200);
[sv addSubview:iv1];
UIImageView *iv2 = [[UIImageView alloc] initWithImage:i1];
iv2.frame = CGRectMake(360*1, 0, 360, 200);
[sv addSubview:iv2];
UIImageView *iv3 = [[UIImageView alloc] initWithImage:i1];
iv3.frame = CGRectMake(360*2, 0, 360, 200);
[sv addSubview:iv3];
l = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
l.text = #"Hello World";
l.backgroundColor = [UIColor blueColor];
[self addSubview:l];
return self;
}
#end
later, in your outer view creation:
[window addSubview:[[MyView alloc] initWithFrame:CGRectMake(0, 20, 360, 200)]];

Resources