[英]UIPageViewController and UIPageControl transparent background color - iOS
[英]UIPageControl - How to make the background transparent?
我正在使用UIPageControl
並嘗試使背景透明。
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
pageControl.backgroundColor = [UIColor blackColor];
有什么建議?
試着
pageControl.backgroundColor = [UIColor clearColor];
沒運氣。
============
好了,通過@ powerj1984和@daniellarsson提到UIPageControl
似乎是相對於定位和背景顏色不可改變。
所以我決定把它移到UIViewController並創建一個UIPageControl
和
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
然后將它帶到我的UIPageViewController
頂部,就像在viewDidLoad
self.pageControl.numberOfPages = self.pageTitles.count;
[self.view addSubview:_pageControl.viewForBaselineLayout];
然后我在viewControllerBeforeViewController
和viewControllerAfterViewController
更新了UIPageControl
的索引
NSUInteger index = ((HomePageContentViewController*) viewController).pageIndex;
self.pageControl.currentPage = index;
好的,正如@ powerj1984和@daniellarsson所提到的,UIPageControl在位置和背景顏色方面似乎無法改變。
所以我決定把它移到UIViewController並創建一個UIPageControl和
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
然后將它帶到我的UIPageViewController頂部,就像在viewDidLoad中一樣
self.pageControl.numberOfPages = self.pageTitles.count;
[self.view addSubview:_pageControl.viewForBaselineLayout];
然后我在viewControllerBeforeViewController和viewControllerAfterViewController中更新了UIPageControl的索引
NSUInteger index = ((HomePageContentViewController*) viewController).pageIndex;
self.pageControl.currentPage = index;
pageControl.backgroundColor = [UIColor clearColor];
正在工作但是!! 你必須在[UIColor clearColor]
上設置UIPageViewController
的backgroundColor
而不是! 您將看到容器視圖的backgroundcolor。
- (void)initPageViewController
{
_pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:nil];
UIImageView *imgView= [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
imgView.image = [UIImage imageNamed:@"NiceBGImage"];
[self.view addSubview:imgView];
self.view.backgroundColor = [UIColor clearColor];
_pageController.view.frame = ({
CGRect frame = [[UIScreen mainScreen] bounds];
frame;
});
_pageController.delegate = self;
_pageController.dataSource = self;
_pageController.view.backgroundColor = [UIColor clearColor];
NSArray *controllers = @[_controllers[0]];
[self.pageController setViewControllers:controllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
pageControl.backgroundColor = [UIColor clearColor];
pageControl = [UIPageControl appearanceWhenContainedIn:[_pageController class], nil];
[self addChildViewController:_pageController];
[[self view] addSubview:_pageController.view];
[self.pageController didMoveToParentViewController:self];
}
頁面控件默認是透明的。
請查看此示例代碼:
UIPageControl *pageControl = [[UIPageControl alloc] init];
pageControl.frame = CGRectMake(10,10,100,100);
pageControl.numberOfPages = 8;
pageControl.currentPage = 0;
[self.view addSubview:pageControl];
[self.view bringSubviewToFront:pageControl];
[self.view setBackgroundColor:[UIColor blackColor]];
請在下面找到更多自定義的可能性
頁面控件的外觀
您可以通過設置下面描述的屬性來自定義頁面控件的外觀。
要自定義應用程序中所有頁面控件的外觀 ,請使用外觀代理 (例如,[UIPageControl外觀])。 有關外觀代理的詳細信息, 請參閱外觀代理 。
色調
自定義頁面控件外觀的唯一方法是為表示每個頁面的點設置自定義色調。 當前頁面(currentPageIndicatorTintColor)字段影響表示當前顯示頁面的點的顏色,並且色調顏色(pageIndicatorTintColor)字段影響表示每個其他頁面的點的顏色。 當前頁面點的默認顏色為白色,其他頁面點的默認顏色為半透明灰色。
如果希望自定義顏色為半透明,則必須指定alpha值小於1.0的顏色。 這必須以編程方式完成,如下例所示:
self.myPageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.5];
self.myPageControl.pageIndicatorTintColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.5];
在UIPageControl - Developer.Apple文檔中查看更多詳細信息
從Xcode 6.1.1和iOS 8.1.1開始,我知道將UIPageControl對象的背景顏色設置為清晰的顏色對我有用。
[[self pageControl] setBackgroundColor:[UIColor clearColor]];
我終於在Swift中找到了解決這個問題的工作方案。 您可以在此鏈接中找到原始代碼和答案。
它要求您覆蓋viewDidLayoutSubviews()函數以將頁面控件帶到視圖的前面。
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let subViews: NSArray = view.subviews
var scrollView: UIScrollView? = nil
var pageControl: UIPageControl? = nil
for view in subViews {
if view.isKindOfClass(UIScrollView) {
scrollView = view as? UIScrollView
}
else if view.isKindOfClass(UIPageControl) {
pageControl = view as? UIPageControl
}
}
if (scrollView != nil && pageControl != nil) {
scrollView?.frame = view.bounds
view.bringSubviewToFront(pageControl!)
}
}
如果您將UIPageControl
的背景顏色設置為與VC的背景顏色相同的背景圖像 ,它可以工作:
[UIPageControl appearance].backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"pageControllerBackground"]];
以下是Juan Ariza的答案
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let subViews: NSArray = view.subviews
var scrollView: UIScrollView? = nil
var pageControl: UIPageControl? = nil
for view in subViews {
if view.isKindOfClass(UIScrollView) {
scrollView = view as? UIScrollView
}
else if view.isKindOfClass(UIPageControl) {
pageControl = view as? UIPageControl
pageControl?.pageIndicatorTintColor = UIColor.lightGrayColor()
pageControl?.currentPageIndicatorTintColor = UIColor.blackColor()
pageControl?.backgroundColor = UIColor.clearColor()
}
}
if (scrollView != nil && pageControl != nil) {
scrollView?.frame = view.bounds
view.bringSubviewToFront(pageControl!)
}
}
試試這個:
pageControl = [UIPageControl appearance];
//for change the bullets color
pageControl.pageIndicatorTintColor = [UIColor colorWithRed:(129.0/255) green:(129.0/255) blue:(129.0/255) alpha:1.0];
pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:(06.0/255) green:(122.0/255) blue:(145.0/255) alpha:1.0];
pageControl.backgroundColor = [UIColor clearColor];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.