更改顏色非常簡單,但可以更改所有未選中點的邊框嗎?如何更改UIPageControl點的邊界?
例:
dot.layer.borderWidth = 0.5 dot.layer.borderColor = UIColor.blackColor()
更改顏色非常簡單,但可以更改所有未選中點的邊框嗎?如何更改UIPageControl點的邊界?
例:
dot.layer.borderWidth = 0.5 dot.layer.borderColor = UIColor.blackColor()
是可以這樣做.. 其實它很簡單。
Pagecontrol由許多可以訪問的子視圖組成。 self.pageControl.subviews返回你的[UIView]即UIView的數組。 獲得單個視圖後,您可以爲其添加邊框,更改其borderColor,更改其邊框寬度,像縮放它一樣轉換點大小。可以使用UIView所具有的所有屬性。
for index in 0..<array.count{ // your array.count
let viewDot = weakSelf?.pageControl.subviews[index]
viewDot?.layer.borderWidth = 0.5
viewDot?.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
if (index == indexPath.row){ // indexPath is the current indexPath of your selected cell or view in the collectionView i.e which needs to be highlighted
viewDot?.backgroundColor = UIColor.black
viewDot?.layer.borderColor = UIColor.black.cgColor
}
else{
viewDot?.backgroundColor = UIColor.white
viewDot?.layer.borderColor = UIColor.black.cgColor
}
}
,它看起來像這樣
記住你不需要設置weakSelf?.pageControl.currentPage = indexPath.row。做讓我知道在任何情況下,問題..希望這可以解決你的問題。 所有最優秀的
嗨!我測試了你的代碼(但在客觀的c),我無法獲得邊界。你能幫我嗎? –
當然。你在哪個方法使用這個代碼? –
我在viewDidLoad中使用了這段代碼 –
擴展集的PageControl指標邊境/斯威夫特3
extension UIImage {
class func outlinedEllipse(size: CGSize, color: UIColor, lineWidth: CGFloat = 1.0) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
guard let context = UIGraphicsGetCurrentContext() else {
return nil
}
context.setStrokeColor(color.cgColor)
context.setLineWidth(lineWidth)
let rect = CGRect(origin: .zero, size: size).insetBy(dx: lineWidth * 0.5, dy: lineWidth * 0.5)
context.addEllipse(in: rect)
context.strokePath()
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
用途:
let image = UIImage.outlinedEllipse(size: CGSize(width: 7.0, height: 7.0), color: .lightGray)
self.pageControl.pageIndicatorTintColor = UIColor.init(patternImage: image!)
self.pageControl.currentPageIndicatorTintColor = .lightGray
我不認爲這是可能的。但是你可以輕鬆地實現你自己的頁面控件,可以做到這一點。 – dasdom